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 ; Count of 1 's ; Count of 0 's upto (cnt1)-th index ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( string s , int n ) { int cnt1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) cnt1 ++ ; } int cnt0 = 0 ; for ( int i = 0 ; i < cnt1 ; i ++ ) { if ( s [ i ] == '0' ) cnt0 ++ ; } return cnt0 ; } int main ( ) { int n = 8 ; string s = "01001011" ; int ans = minOperation ( s , n ) ; cout << ans << endl ; }