Given a binary string S. Find the minimum number of operations required to be performed on the number zero to convert it to the number represented by S.
It is allowed to perform operations of 2 types:
- Add 2x
- Subtract 2x
Note: Start performing operations on 0.
Input : 100
Output : 1
Explanation: We just perform a single operation, i.e Add 4 (2^2)
Input : 111
Output : 2
Explanation: We perform the following two operations:
1) Add 8(2^3)
2) Subtract 1(2^0)
The idea is to use Dynamic Programming to solve this problem.
Note: In the below analysis, we considered that all binary strings are represented from LSB to MSB (from LHS to RHS) i.e 2(Binary form: “10”) is represented as “01”.
Let the given binary string be S.
Let dp[i] represents the minimum number of operations required to make a binary string R such that R[0…i] is same as S[0…i] and R[i+1…] = “00..0”
Similarly let dp[i] represents the minimum number of operations required to make a binary string R such that R[0…i] is same as S[0…i] and R[i+1…] = “11..1”
If Si is ‘0’, then dp[i] = dp[i – 1]. Since we do not require any additional operation. Now we consider the value of dp[i] which is a bit trickier. For dp[i] we can either make our transition from dp[i – 1] by just making the ith character of the string formed by dp[i-1], ‘0’. Since earlier this ith character was “1”. We just need to subtract 2i from the string represented by dp[i-1]. Thus we perform one operation other than the ones represented by dp[i-1].
The other transition can be from dp[i-1]. Let dp[i-1] represents the string R. Then we need to keep R[i] = 0 as it already is but R[i + 1…..] which is currently “000..0”, needs to be to be changed to “111…1”, this can be done by subtracting 2(i+1) from R. Thus we just need one operation other than the ones represented by dp[i-1].
Similar is the case when Si is ‘1’.
The final answer is the one represented by dp[l-1], where l is the length of the binary string S.
Below is the implementation of the above approach:
Time Complexity: O(n)
Auxiliary Space: O(n)
- Minimum number of given operations required to convert a string to another string
- Minimum swaps required to convert one binary string to another
- Minimum swaps required to make a binary string alternating
- Minimum operations required to make all the elements distinct in an array
- Operations required to make the string empty
- Count the number of carry operations required to add two numbers
- Find the number of operations required to make all array elements Equal
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum adjacent swaps required to Sort Binary array
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Count of operations to make a binary string"ab" free
- Number of steps required to convert a binary number to one
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum flips required to maximize a number with k set bits