Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | C ++ program for the above approach ; Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isOnesGreater ( string S , int N ) { int cnt0 = 0 ; int cnt1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) cnt1 ++ ; else cnt0 ++ ; } for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { if ( i > 0 && S [ i - 1 ] == '0' ) { S [ i - 1 ] = ' _ ' ; cnt0 -- ; } else if ( i < N && S [ i + 1 ] == '0' ) { S [ i + 1 ] = ' _ ' ; cnt0 -- ; } } } if ( cnt1 > cnt0 ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { string S = "01" ; int N = S . length ( ) ; isOnesGreater ( S , N ) ; return 0 ; }