Minimize flips to make binary string as all 1 s by flipping characters in substring of size K repeatedly | Function to find the minimum number of operations required to convert all the characters to 1 by flipping the substrings of size K ; Stores the minimum number of operations required ; Traverse the string S ; If the character is 0 ; Flip the substrings of size K starting from i ; Increment the minimum count of operations required ; After performing the operations check if string S contains any 0 s ; If S contains only 1 's ; Driver Code
function minOperation ( St , K , N ) { S = St . split ( ' ' ) ; let min = 0 ; let i ; for ( i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' && i + K <= N ) { for ( let j = i ; j < i + K ; j ++ ) { if ( S [ j ] == ' ' ) S [ j ] = ' ' ; else S [ j ] = ' ' ; } min ++ ; } } for ( i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) break ; } if ( i == N ) document . write ( min ) ; else document . write ( - 1 ) ; } let S = " " ; let K = 3 ; let N = S . length ; minOperation ( S , K , N ) ;