Make a given Binary String non | C ++ program for the above approach ; Function to return the length of smallest subsequence required to be removed to make the given string non - decreasing ; Length of the string ; Count of zeros and ones ; Traverse the string ; Count minimum removals to obtain strings of the form "00000 . . . . " or "11111 . . . " ; Increment count ; Remove 1 s and remaining 0 s ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_length ( string str ) { int n = str . length ( ) ; int total_zeros = 0 ; int total_ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) total_zeros ++ ; else total_ones ++ ; } int ans = min ( total_zeros , total_ones ) ; int cur_zeros = 0 , cur_ones = 0 ; for ( char x : str ) { if ( x == '0' ) cur_zeros ++ ; else cur_ones ++ ; ans = min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } cout << ans ; } int main ( ) { string str = "10011" ; min_length ( str ) ; return 0 ; }