Minimum flips to make all 1 s in right and 0 s in left | Function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; Stores length of str ; Store count of 0 s in the string ; Traverse the string ; If current character is 0 ; Update zeros ; If count of 0 s in the string is 0 or n ; Store minimum count of flips required to make all 0 s on the left and 1 s on the right ; Stores count of 1 s on the left side of each index ; Stores count of flips required to make all 0 s on the left and 1 s on the right ; Traverse the string ; If current character is 1 ; Update currOnes ; Update flips ; Update the minimum count of flips ; Driver Code
function minimumCntOfFlipsRequired ( str ) { let n = str . length ; let zeros = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { zeros ++ ; } } if ( zeros == 0 zeros == n ) { return 0 ; } let minFlips = Number . MAX_VALUE ; let currOnes = 0 ; let flips ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { currOnes ++ ; } flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; minFlips = Math . min ( minFlips , flips ) ; } return minFlips ; } let s1 = " " ; document . write ( minimumCntOfFlipsRequired ( s1 ) ) ;