Minimum number of operations required to maximize the Binary String | C ++ Program to implement the above approach ; Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( string s , int n ) { int ans = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( s [ i ] == '0' && s [ j ] == '1' ) { ans ++ ; i ++ ; j -- ; continue ; } if ( s [ i ] == '1' ) { i ++ ; } if ( s [ j ] == '0' ) { j -- ; } } return ans ; } int main ( ) { int n = 8 ; string s = "10100101" ; int ans = minOperation ( s , n ) ; cout << ans << endl ; }