Minimum changes required such that the string satisfies the given condition | Function to return the minimum changes required ; To store the count of minimum changes , number of ones and the number of zeroes ; First character has to be '1' ; If condition fails changes need to be made ; Return the required count ; Driver Code
function minChanges ( str , n ) { let count = 0 , zeros = 0 , ones = 0 ; if ( str [ 0 ] != ' ' ) { count ++ ; ones ++ ; } for ( let i = 1 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) zeros ++ ; else ones ++ ; if ( zeros > ones ) { zeros -- ; ones ++ ; count ++ ; } } return count ; } let str = " " . split ( ' ' ) ; let n = str . length ; document . write ( minChanges ( str , n ) ) ;