Minimize count of flips required such that no substring of 0 s have length exceeding K | Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current subString ; If current character is 0 ; Continue ongoing subString ; Start a new subString ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
function min_flips ( str , k ) { if ( str . length == 0 ) return 0 ; let ans = 0 ; let cnt_zeros = 0 ; for ( let ch in str . split ( ' ' ) ) { if ( str [ ch ] == ' ' ) { ++ cnt_zeros ; } else { cnt_zeros = 0 ; } if ( cnt_zeros == k ) { ++ ans ; cnt_zeros = 0 ; } } return ans ; } let str = " " ; let k = 3 ; document . write ( min_flips ( str , k ) ) ;