Min flips of continuous characters to make all characters same in a string | CPP program to find min flips in binary string to make all characters equal ; To find min number of flips in binary string ; If last character is not equal to str [ i ] increase res ; To return min flips ; Driver program to check findFlips ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findFlips ( char str [ ] , int n ) { char last = ' ▁ ' ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( last != str [ i ] ) res ++ ; last = str [ i ] ; } return res / 2 ; } int main ( ) { char str [ ] = "00011110001110" ; int n = strlen ( str ) ; cout << findFlips ( str , n ) ; return 0 ; }