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
< ? php function findZeroes ( $ arr , $ n , $ m ) { $ wL = 0 ; $ wR = 0 ; $ bestL = 0 ; $ bestWindow = 0 ; $ 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 ( $ i = 0 ; $ i < $ bestWindow ; $ i ++ ) { if ( $ arr [ $ bestL + $ i ] == 0 ) echo $ bestL + $ i . " ▁ " ; } } $ arr = array ( 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ) ; $ m = 2 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Indexes ▁ of ▁ zeroes ▁ to ▁ be ▁ flipped ▁ are ▁ " ; findZeroes ( $ arr , $ n , $ m ) ; return 0 ; ? >