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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minKBitFlips ( int A [ ] , int K , int N ) { int temp [ N ] ; memset ( temp , 0 , N ) ; int count = 0 ; int flip = 0 ; count ++ ; for ( int i = 0 ; i < N ; ++ i ) { flip ^= temp [ i ] ; if ( A [ i ] == flip ) { count ++ ; if ( i + K > N ) { return -1 ; } flip ^= 1 ; if ( i + K < N ) { temp [ i + K ] ^= 1 ; } } } return count ; } int main ( ) { int A [ ] = { 0 , 1 , 0 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 1 ; int ans = minKBitFlips ( A , K , N ) ; cout << ans ; return 0 ; }