Given a binary number as a string str of length L. The task is to find the minimum number of operations needed so that the number becomes 2L-1, that is a string consisting of only 1’s of the length L.
In each operation, the number N can be replaced by N xor (N + 1).
Input: str = “10010111”
N = 10010111, N + 1 = 10011000, so N xor (N + 1) = 00001111
N = 00001111, N + 1 = 00010000, so N xor (N + 1) = 00011111
N = 00011111, N + 1 = 00100000, so N xor (N + 1) = 00111111
N = 00111111, N + 1 = 01000000, so N xor (N + 1) = 01111111
N = 01111111, N + 1 = 10000000, so N xor (N + 1) = 11111111
Input: str = “101000100101011101”
Approach: After performing the given operation, it can be observed that in order to get the required number, in the end, the number of operations will be:
Number of Operations = length of the string (after removing leading 0s) – count of consecutive 1’s form the end (starting from the least significant bit)
Below is the implementation of the above approach:
Time Complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum operations required to convert all characters of a String to a given Character
- Minimum number of given operations required to convert a string to another string
- Minimum number of operations required to obtain a given Binary String
- Minimum number of operations required to sum to binary string S
- Minimum number of operations required to maximize the Binary String
- Minimum swaps required to convert one binary string to another
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum operations required to make the string satisfy the given condition
- Minimum flips required to keep all 1s together in a Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum reduce operations to convert a given string into a palindrome
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum number of given operations required to make two strings equal
- Minimum operations of the given type required to make a complete graph
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.