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
using System ; class GFG { public static void 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 = Math . Min ( total_zeros , total_ones ) ; int cur_zeros = 0 , cur_ones = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { char x = str [ i ] ; if ( x == '0' ) { cur_zeros ++ ; } else { cur_ones ++ ; } ans = Math . Min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } Console . WriteLine ( ans ) ; } static public void Main ( ) { string str = "10011" ; min_length ( str ) ; } }