Given an integer N, the task is to find the minimum number of bits required to be flipped to convert the binary representation of N into a palindrome.
Input: N = 12
Binary String representing 12 = “1100”.
To make “1100” a palindrome, convert the string to “0110”.
Therefore, minimum bits required to be flipped is 2.
Input: N = 7
Binary String representing 7 = 111, which is already a palindrome.
Naive Approach: The simplest way is to check for every possible subset that is a palindrome having the same number of bits.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized through these steps:
- At first check the length of the binary form of the given number.
- Take two pointers to one at the L.S.B and another to the M.S.B.
- Now keep decrementing the first pointer and incrementing the second pointer.
- Check whether the bits at both the position of first and the second pointer is same or not. If not, increment the number of bits to change.
Below is the implementation of the above approach:
Time Complexity: O(log N)
Auxiliary Space: O(1)
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 bit flips such that every K consecutive bits contain at least one set bit
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Queries to count minimum flips required to fill a binary submatrix with 0s only
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimum flips required to keep all 1s together in a Binary string
- Minimum substring flips required to convert given binary string to another
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimum number of flips with rotation to make binary string alternating
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Make palindromic string non-palindromic by rearranging its letters
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Number of flips to make binary string alternate | Set 1
- Find Bit whose minimum sequence flips makes all bits same
- Count of 1-bit and 2-bit characters in the given binary string
- Minimum increments required to make given matrix palindromic
- Minimum flips required to maximize a number with k set bits
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Minimum number of flips required such that the last cell of matrix can be reached from any other cell
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.