Max Consecutive Ones III
Problem Statement
You are given an array nums that contains only 0s and 1s, and a number k.
You may change at most k of the 0s into 1s. After doing that, what is the longest run of consecutive 1s you can get?
A binary array nums and an integer k.
The length of the longest block of consecutive 1s possible after flipping at most k zeros.
Flip the zeros at positions 4 and 5 (0-based). Then positions 4 to 9 are all 1s: [0,0,1,1,1,1] becomes six 1s in a row.
Flip the zeros at positions 4, 5 and 9. Then positions 2 to 11 are all 1s — length 10.
- • 1 ≤ nums.length ≤ 10^5
- • nums[i] is 0 or 1
- • 0 ≤ k ≤ nums.length
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each start, extend right while the zero count stays ≤ k
Grow right; while zeros > k, shrink from the left; track the longest window
For every start, walk right and count the zeros. As long as the count is at most k, this block can be turned into all 1s, so its length is a candidate. Once the count goes over k, stop — longer blocks from this start only have more zeros.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.