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
import java . io . * ; import java . util . * ; class GFG { static int minKBitFlips ( int [ ] A , int K ) { int [ ] temp = new int [ A . length ] ; int count = 0 ; int flip = 0 ; for ( int 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 ; } public static void main ( String [ ] args ) { int [ ] A = { 0 , 1 , 0 } ; int K = 1 ; int ans = minKBitFlips ( A , K ) ; System . out . println ( ans ) ; } }