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 given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum number of given operations required to convert a string to another string
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Find the minimum number of operations required to make all array elements equal
- Minimum swaps required to convert one binary string to another
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum increment or decrement operations required to make the array sorted
- Operations required to make the string empty
- Count the number of carry operations required to add two numbers
- Minimum number of swaps to make two binary string equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.