Minimum removals required to place all 0 s before 1 s in a Binary String | 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
function minimumDeletions ( s ) { var right_0 = 0 ; var i ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) right_0 ++ ; } var left_1 = 0 ; var n = s . length ; var res = 2147483647 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { right_0 -= 1 ; } else { left_1 += 1 ; } res = Math . min ( res , right_0 + left_1 ) ; } return res ; } var s = " " ; var count = minimumDeletions ( s ) ; document . write ( count ) ;