Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Maintain a window where the number of zeros does not exceed k. Expand right and count zeros. When zero count exceeds k, shrink from the left until the count is valid again. The window length at each step represents a valid sequence of 1s with at most k flips.
The window invariant guarantees at most k zeros inside, meaning all zeros in the window can be flipped to 1s. By tracking zero count instead of actually flipping, we efficiently find the longest valid subarray in a single pass.
Updated Feb 2026