Minimum number of flips to make a Binary String increasing | C ++ program for the above approach ; ; Function to find the minimum number of flips required to make string increasing ; Length of s ; Total number of zero in s ; Stores count of 1 s till ith index ; stores the minimum count of flip ; Traverse the given string ; Update the value of res and count of 1 s ; Return the minimum number of flips ; Driver code ; Given string ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumFlips ( string s ) { int n = s . size ( ) ; int cnt0 = count ( s . begin ( ) , s . end ( ) , '0' ) ; int cnt1 = 0 ; int res = n - cnt0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { cnt0 -= 1 ; } else if ( s [ i ] == '1' ) { res = min ( res , cnt1 + cnt0 ) ; cnt1 ++ ; } } return res ; } int main ( ) { string S = "000110" ; cout << minimumFlips ( S ) ; return 0 ; }