Check if all bits can be made same by flipping two consecutive bits | C ++ program for the above approach ; Function to check if Binary string can be made equal ; Counting occurence of zero and one in binary string ; From above observation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string canMake ( string & s ) { int o = 0 , z = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] - '0' == 1 ) o ++ ; else z ++ ; } if ( o % 2 == 1 && z % 2 == 1 ) return " NO " ; else return " YES " ; } int main ( ) { string s = "01011" ; cout << canMake ( s ) << ' ' ; return 0 ; }