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 . Enqueue ( i ) ; insert ; Driver Code
using System ; using System . Collections ; class GFG { static int minKBitFlips ( int [ ] A , int K ) { Queue flip = new Queue ( ) ; int count = 0 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( flip . Count > 0 && ( i - ( int ) flip . Peek ( ) >= K ) ) { flip . Dequeue ( ) ; } if ( A [ i ] % 2 == flip . Count % 2 ) { if ( i + K - 1 >= A . Length ) { return - 1 ; } count ++ ; } } return count ; } } static void ( ) { int [ ] A = { 0 , 1 , 0 } ; int K = 1 ; int ans = minKBitFlips ( A , K ) ; Console . WriteLine ( ans ) ; }