Minimum Count of Bit flips required to make a Binary String Palindromic
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.
Examples:
Input: N = 12
Output: 2
Explanation:
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
Output: 0
Explanation:
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 the first and the second pointer are the same or not. If not, increment the number of bits to change.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int check_length( int n)
{
int ans = 0;
while (n) {
n = n >> 1;
ans++;
}
return ans;
}
int check_ith_bit( int n, int i)
{
return (n & (1 << (i - 1)))
? true
: false ;
}
int no_of_flips( int n)
{
int len = check_length(n);
int ans = 0;
int right = 1;
int left = len;
while (right < left) {
if (check_ith_bit(n, right)
!= check_ith_bit(n, left))
ans++;
left--;
right++;
}
return ans;
}
int main()
{
int n = 12;
cout << no_of_flips(n);
return 0;
}
|
Java
class GFG{
static int check_length( int n)
{
int ans = 0 ;
while (n != 0 )
{
n = n >> 1 ;
ans++;
}
return ans;
}
static boolean check_ith_bit( int n, int i)
{
return (n & ( 1 << (i - 1 ))) != 0 ? true : false ;
}
static int no_of_flips( int n)
{
int len = check_length(n);
int ans = 0 ;
int right = 1 ;
int left = len;
while (right < left)
{
if (check_ith_bit(n, right) !=
check_ith_bit(n, left))
ans++;
left--;
right++;
}
return ans;
}
public static void main(String[] args)
{
int n = 12 ;
System.out.println(no_of_flips(n));
}
}
|
Python3
def check_length(n):
ans = 0
while (n):
n = n >> 1
ans + = 1
return ans
def check_ith_bit(n, i):
if (n & ( 1 << (i - 1 ))):
return True
else :
return False
def no_of_flips(n):
ln = check_length(n)
ans = 0
right = 1
left = ln
while (right < left):
if (check_ith_bit(n, right) ! =
check_ith_bit(n, left)):
ans + = 1
left - = 1
right + = 1
return ans
n = 12
print (no_of_flips(n))
|
C#
using System;
class GFG{
static int check_length( int n)
{
int ans = 0;
while (n != 0)
{
n = n >> 1;
ans++;
}
return ans;
}
static bool check_ith_bit( int n, int i)
{
return (n & (1 << (i - 1))) != 0 ?
true : false ;
}
static int no_of_flips( int n)
{
int len = check_length(n);
int ans = 0;
int right = 1;
int left = len;
while (right < left)
{
if (check_ith_bit(n, right) !=
check_ith_bit(n, left))
ans++;
left--;
right++;
}
return ans;
}
public static void Main(String[] args)
{
int n = 12;
Console.WriteLine(no_of_flips(n));
}
}
|
Javascript
<script>
function check_length(n)
{
let ans = 0;
while (n != 0)
{
n = n >> 1;
ans++;
}
return ans;
}
function check_ith_bit(n, i)
{
return (n & (1<< (i - 1))) != 0 ? true : false ;
}
function no_of_flips(n)
{
let len = check_length(n);
let ans = 0;
let right = 1;
let left = len;
while (right < left)
{
if (check_ith_bit(n, right) !=
check_ith_bit(n, left))
ans++;
left--;
right++;
}
return ans;
}
let n = 12;
document.write(no_of_flips(n));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
16 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...