Skip to content
Veltoryx Technologies
ProProblem 26● MediumLeetCode #1004 ↗3 min readUpdated 30 October 2026

Max Consecutive Ones III

Find the longest run of 1s you can make by flipping at most k zeros, with a window that grows right and shrinks left.

01The problem

nums holds only 0s and 1s. You may flip at most k zeros into ones. Return the length of the longest run of 1s you can make. In other words: the longest window containing at most k zeros.

InputOutput
[1,1,1,0,0,0,1,1,1,1,0], k = 26
[0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 310

Constraints: 1 to 100,000 values, 0 ≤ k ≤ n.

Loading the rest of the lesson…