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 cuts required to convert a palindromic string to a different palindromic string
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- 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 flips required to keep all 1s together in a Binary string
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- 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
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Make palindromic string non-palindromic by rearranging its letters
- Number of flips to make binary string alternate | Set 1
- Find Bit whose minimum sequence flips makes all bits same
- Minimum flips required to maximize a number with k set bits
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Count of 1-bit and 2-bit characters in the given binary string
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Min flips of continuous characters to make all characters same in a string
- Minimum swaps required to make a binary string alternating
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.