Minimum operations required to convert a binary string to all 0 s or all 1 s | C ++ implementation of the approach ; Function to return the count of minimum operations required ; Increment count when consecutive characters are different ; Answer is rounding off the ( count / 2 ) to lower ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string str , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] != str [ i + 1 ] ) count ++ ; } return ( count + 1 ) / 2 ; } int main ( ) { string str = "000111" ; int n = str . length ( ) ; cout << minOperations ( str , n ) ; return 0 ; }