Minimum Count of Bit flips required to make a Binary String Palindromic | 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 ; Poleter to the LSB ; Poleter to the MSB ; Check if the bits are equal ; Decrementing the left poleter ; Incrementing the right poleter ; Returns the number of bits to flip . ; Driver Code
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 ) ) ;