Minimum Count of Bit flips required to make a Binary String Palindromic
Last Updated :
16 Nov, 2021
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++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the
// length of the binary string
int check_length(int n)
{
// Length
int ans = 0;
while (n) {
// Right shift of n
n = n >> 1;
// Increment the length
ans++;
}
// Return the length
return ans;
}
// Function to check if the bit present
// at i-th position is a set bit or not
int check_ith_bit(int n, int i)
{
// Returns true if the bit is set
return (n & (1 << (i - 1)))
? true
: false;
}
// Function to count the minimum
// number of bit flips required
int no_of_flips(int n)
{
// Length of the binary form
int len = check_length(n);
// Number of flips
int ans = 0;
// Pointer to the LSB
int right = 1;
// Pointer to the MSB
int left = len;
while (right < left) {
// Check if the bits are equal
if (check_ith_bit(n, right)
!= check_ith_bit(n, left))
ans++;
// Decrementing the
// left pointer
left--;
// Incrementing the
// right pointer
right++;
}
// Returns the number of
// bits to flip.
return ans;
}
// Driver Code
int main()
{
int n = 12;
cout << no_of_flips(n);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to calculate the
// length of the binary string
static int check_length(int n)
{
// Length
int ans = 0;
while (n != 0)
{
// Right shift of n
n = n >> 1;
// Increment the length
ans++;
}
// Return the length
return ans;
}
// Function to check if the bit present
// at i-th position is a set bit or not
static boolean check_ith_bit(int n, int i)
{
// Returns true if the bit is set
return (n & (1<< (i - 1))) != 0 ? true : false;
}
// Function to count the minimum
// number of bit flips required
static int no_of_flips(int n)
{
// Length of the binary form
int len = check_length(n);
// Number of flips
int ans = 0;
// Pointer to the LSB
int right = 1;
// Pointer to the MSB
int left = len;
while (right < left)
{
// Check if the bits are equal
if (check_ith_bit(n, right) !=
check_ith_bit(n, left))
ans++;
// Decrementing the
// left pointer
left--;
// Incrementing the
// right pointer
right++;
}
// Returns the number of
// bits to flip.
return ans;
}
// Driver Code
public static void main(String[] args)
{
int n = 12;
System.out.println(no_of_flips(n));
}
}
// This code is contributed by rutvik_56
Python3
# Python3 program to implement
# the above approach
# Function to calculate the
# length of the binary string
def check_length(n):
# Length
ans = 0
while (n):
# Right shift of n
n = n >> 1
# Increment the length
ans += 1
# Return the length
return ans
# Function to check if the bit present
# at i-th position is a set bit or not
def check_ith_bit(n, i):
# Returns true if the bit is set
if (n & (1 << (i - 1))):
return True
else:
return False
# Function to count the minimum
# number of bit flips required
def no_of_flips(n):
# Length of the binary form
ln = check_length(n)
# Number of flips
ans = 0
# Pointer to the LSB
right = 1
# Pointer to the MSB
left = ln
while (right < left):
# Check if the bits are equal
if (check_ith_bit(n, right) !=
check_ith_bit(n, left)):
ans += 1
# Decrementing the
# left pointer
left -= 1
# Incrementing the
# right pointer
right += 1
# Returns the number of
# bits to flip.
return ans
# Driver Code
n = 12
print(no_of_flips(n))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to calculate the
// length of the binary string
static int check_length(int n)
{
// Length
int ans = 0;
while (n != 0)
{
// Right shift of n
n = n >> 1;
// Increment the length
ans++;
}
// Return the length
return ans;
}
// Function to check if the bit present
// at i-th position is a set bit or not
static bool check_ith_bit(int n, int i)
{
// Returns true if the bit is set
return (n & (1 << (i - 1))) != 0 ?
true : false;
}
// Function to count the minimum
// number of bit flips required
static int no_of_flips(int n)
{
// Length of the binary form
int len = check_length(n);
// Number of flips
int ans = 0;
// Pointer to the LSB
int right = 1;
// Pointer to the MSB
int left = len;
while (right < left)
{
// Check if the bits are equal
if (check_ith_bit(n, right) !=
check_ith_bit(n, left))
ans++;
// Decrementing the
// left pointer
left--;
// Incrementing the
// right pointer
right++;
}
// Returns the number of
// bits to flip.
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int n = 12;
Console.WriteLine(no_of_flips(n));
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// JavaScript program for the above approach
// Function to calculate the
// length of the binary string
function check_length(n)
{
// Length
let ans = 0;
while (n != 0)
{
// Right shift of n
n = n >> 1;
// Increment the length
ans++;
}
// Return the length
return ans;
}
// Function to check if the bit present
// at i-th position is a set bit or not
function check_ith_bit(n, i)
{
// Returns true if the bit is set
return (n & (1<< (i - 1))) != 0 ? true : false;
}
// Function to count the minimum
// number of bit flips required
function no_of_flips(n)
{
// Length of the binary form
let len = check_length(n);
// Number of flips
let ans = 0;
// Pointer to the LSB
let right = 1;
// Pointer to the MSB
let left = len;
while (right < left)
{
// Check if the bits are equal
if (check_ith_bit(n, right) !=
check_ith_bit(n, left))
ans++;
// Decrementing the
// left pointer
left--;
// Incrementing the
// right pointer
right++;
}
// Returns the number of
// bits to flip.
return ans;
}
// Driver Code
let n = 12;
document.write(no_of_flips(n));
</script>
Time Complexity: O(log N)
Auxiliary Space: O(1)
Similar Reads
Count minimum swap to make string palindrome Given a string S, the task is to find out the minimum no of adjacent swaps required to make string s palindrome. If it is not possible, then return -1. Examples: Input: aabcb Output: 3 Explanation: After 1st swap: abacb After 2nd swap: abcab After 3rd swap: abcba Input: adbcdbad Output: -1 ApproachT
6 min read
Minimum prefixes required to be flipped to convert a Binary String to another Given two binary strings A and B of length N, the task is to convert the string from A to string B by repeatedly flipping all the bits of a prefix of A, i.e. convert all the 0s in the prefix to 1s and vice-versa and print the minimum number of prefix flips required and the length of respective prefi
9 min read
Minimum non-adjacent pair flips required to remove all 0s from a Binary String The problem statement asks us to find the minimum number of operations required to remove all 0s from a given binary string S. An operation involves flipping at most two non-adjacent characters (i.e., characters that are not next to each other) in the string. Let's take the first example given: S =
11 min read
Minimum number of deletions to make a string palindrome | Set 2 Given a string A, compute the minimum number of characters you need to delete to make resulting string a palindrome. Examples: Input : baca Output : 1 Input : geek Output : 2 We have discussed one approach in below post. Minimum number of deletions to make a string palindrome Below approach will use
9 min read
Minimum cost of flipping characters required to convert Binary String to 0s only Given binary string str, and integers A, which denotes the cost of converting consecutive 1s to 0s, and B, which denotes the cost of converting 0s to 1s. The task is to find the minimum cost to reduce the string str to 0s only. Examples: Input: str = â01101110â, A = 5, B = 1Output: 6Explanation:Conv
8 min read