Minimum removals required to place all 0 s before 1 s in a Binary String | C ++ program for the above approach ; Function to count minimum removals required to arrange all 0 s before 1 s ; Count the occurences of 0 in s ; Size of the string ; Stores the minimum number of removals required ; Iterate over each of the characters in the string s ; If the i - th character is found to be '0' ; Store the minimum of res and right_0 + left_1 in res ; Return the final result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumDeletions ( string s ) { int right_0 = count ( s . begin ( ) , s . end ( ) , '0' ) ; int left_1 = 0 ; int n = s . size ( ) ; int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) { right_0 -= 1 ; } else { left_1 += 1 ; } res = min ( res , right_0 + left_1 ) ; } return res ; } int main ( ) { string s = "001101" ; int count = minimumDeletions ( s ) ; cout << count ; return 0 ; }