2Brute2Best
🔥0

Max Consecutive Ones III

Medium·LeetCode #1004 Pro OnlyMedium
LeetCode
01

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?

Input

A binary array nums and an integer k.

Output

The length of the longest block of consecutive 1s possible after flipping at most k zeros.

Example 1
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6

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.

Example 2
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10

Flip the zeros at positions 4, 5 and 9. Then positions 2 to 11 are all 1s — length 10.

Constraints
  • • 1 ≤ nums.length ≤ 10^5
  • • nums[i] is 0 or 1
  • • 0 ≤ k ≤ nums.length
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“consecutive 1s”
→ The answer is a contiguous block → think window.
“maximum number”
→ Longest window → shrink while invalid, record after fixing.
“flip at most k 0s”
→ A disguise. It just means: the window may contain at most k zeros.
03

Approach Complexity Ladder

Brute Force — try every startO(N²)O(1)

For each start, extend right while the zero count stays ≤ k

Optimal — variable sliding windowO(N)O(1)

Grow right; while zeros > k, shrink from the left; track the longest window

Brute Force Intuition

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:

Window grows and shrinks. Expand right; when the window breaks the rule, move left until it is valid again. Works when "bigger window = harder to be valid".
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.