Find zeroes to be flipped so that number of consecutive 1 's is maximized | m is maximum of number zeroes allowed to flip n is size of array ; Left and right indexes of current window ; Left index and size of the widest window ; Count of zeroes in current window ; While right boundary of current window doesn 't cross  right end ; If zero count of current window is less than m , widen the window toward right ; If zero count of current window is more than m , reduce the window from left ; Updqate widest window if this window size is more ; Print positions of zeroes in the widest window ; Driver Code
function findZeroes ( arr , n , m ) { let wL = 0 ; let wR = 0 ; let bestL = 0 ; let bestWindow = 0 ; let zeroCount = 0 ; while ( wR < n ) { if ( zeroCount <= m ) { if ( arr [ wR ] == 0 ) zeroCount ++ ; wR ++ ; } if ( zeroCount > m ) { if ( arr [ wL ] == 0 ) zeroCount -- ; wL ++ ; } if ( ( wR - wL > bestWindow ) && ( zeroCount <= m ) ) { bestWindow = wR - wL ; bestL = wL ; } } for ( let i = 0 ; i < bestWindow ; i ++ ) { if ( arr [ bestL + i ] == 0 ) document . write ( bestL + i + " " ) ; } } let arr = new Array ( 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ) ; let m = 2 ; let n = arr . length ; document . write ( " " ) ; findZeroes ( arr , n , m ) ;