Minimum number of flipping adjacent bits required to make given Binary Strings equal | C ++ program for the above approach ; Function to find the minimum number of inversions required . ; Initializing the answer ; Iterate over the range ; If s1 [ i ] != s2 [ i ] , then inverse the characters at i snd ( i + 1 ) positions in s1 . ; Adding 1 to counter if characters are not same ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_Min_Inversion ( int n , string s1 , string s2 ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( s1 [ i ] == '1' ) { s1 [ i ] = '0' ; } else { s1 [ i ] = '1' ; } if ( s1 [ i + 1 ] == '1' ) { s1 [ i + 1 ] = '0' ; } else { s1 [ i + 1 ] = '1' ; } count ++ ; } } if ( s1 == s2 ) { return count ; } return -1 ; } int main ( ) { int n = 4 ; string s1 = "0101" ; string s2 = "1111" ; cout << find_Min_Inversion ( n , s1 , s2 ) << endl ; return 0 ; }