Minimize flips on K |  ; store previous flip events ; remove an item which is out range of window . ; In a window , if A [ i ] is a even number with even times fliped , it need to be fliped again . On other hand , if A [ i ] is a odd number with odd times fliped , it need to be fliped again . ; flip . offer ( i ) ; insert ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int minKBitFlips ( int [ ] A , int K ) { Queue < Integer > flip = new LinkedList < > ( ) ; int count = 0 ; for ( int i = 0 ; i < A . length ; i ++ ) { if ( ! flip . isEmpty ( ) && ( i - flip . peek ( ) >= K ) ) { flip . poll ( ) ; } if ( A [ i ] % 2 == flip . size ( ) % 2 ) { if ( i + K - 1 >= A . length ) { return - 1 ; } count ++ ; } } 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 ) ; } }