Minimum Count of Bit flips required to make a Binary String Palindromic | Java program to implement the above approach ; Function to calculate the length of the binary string ; Length ; Right shift of n ; Increment the length ; Return the length ; Function to check if the bit present at i - th position is a set bit or not ; Returns true if the bit is set ; Function to count the minimum number of bit flips required ; Length of the binary form ; Number of flips ; Pointer to the LSB ; Pointer to the MSB ; Check if the bits are equal ; Decrementing the left pointer ; Incrementing the right pointer ; Returns the number of bits to flip . ; Driver Code
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 ) ) ; } }