Minimize flips on K |  ; We 're flipping the subarray from A[i] to A[i+K-1]  ; If not flip entire array , impossible ; Driver Code
function minKBitFlips ( A , K ) { let temp = new Array ( A . length ) ; let count = 0 ; let flip = 0 ; for ( let i = 0 ; i < A . length ; ++ i ) { flip ^= temp [ i ] ; if ( A [ i ] == flip ) { count ++ ; if ( i + K > A . length ) { return - 1 ; } flip ^= 1 ; if ( i + K < A . length ) { temp [ i + K ] ^= 1 ; } } } return count ; } let A = [ 0 , 1 , 0 ] ; let K = 1 ; let ans = minKBitFlips ( A , K ) ; document . write ( ans ) ;