Segregate 1 s and 0 s in separate halves of a Binary String | C ++ program for the above approach ; Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Driver Code ; Given string ; Length of the string ; Prints the minimum count of operations required
#include <bits/stdc++.h> NEW_LINE using namespace std ; #include <bits/stdc++.h> NEW_LINE using namespace std ; void minOps ( string s , int N ) { int ans = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( s [ i ] != s [ i - 1 ] ) { ans ++ ; } } if ( ans % 2 == 1 ) { cout << ( ans - 1 ) / 2 << endl ; return ; } cout << ( ans / 2 ) ; } int main ( ) { string str = "01011100" ; int N = str . size ( ) ; minOps ( str , N ) ; return 0 ; }