Introduction
LeetCode 1004: Max Consecutive Ones III is a classic sliding window problem that challenges your understanding of arrays, window manipulation, and frequency counting.
The goal is to find the longest subarray of consecutive 1's in a binary array if you are allowed to flip at most K zeros to 1’s.
This problem is an excellent example of transforming a brute-force solution into a linear-time efficient approach using the sliding window pattern.
If you’d like to try solving the problem first, you can attempt it here:
Try the problem on LeetCode:
https://leetcode.com/problems/max-consecutive-ones-iii/
Problem Understanding
You are given:
- A binary array
numscontaining only 0's and 1's - An integer
krepresenting the maximum number of zeros you can flip
You need to return the length of the longest contiguous subarray of 1's after flipping at most k zeros.
Examples:
A naive approach would check every possible subarray, flip zeros, and count consecutive ones:
- Time Complexity: O(n²) → too slow for large arrays
- Inefficient for constraints up to 10⁵ elements
Key Idea: Sliding Window with Zero Count
Instead of brute-force, notice that:
- We only care about how many zeros are in the current window
- We can maintain a sliding window
[i, j] - Keep a counter
zfor zeros in the window - Expand the window by moving
j - If
zexceedsk, shrink the window from the left by movingiand decrementingzfor each zero removed
Intuition:
- The window always contains at most
kzeros - The length of the window gives the maximum consecutive ones achievable with flips
This allows linear traversal of the array with O(1) space, making it optimal.
Approach (Step-by-Step)
- Initialize pointers:
i = 0,j = 0 - Initialize
z = 0(zeros count in current window) andco = 0(max length) - Iterate
jfrom 0 tonums.length - 1: - If
nums[j] == 0, incrementz - Check
z: - If
z <= k: window is valid → updateco = max(co, j - i + 1) - Else: shrink window by moving
iuntilz <= k, decrementzfor zeros leaving window - Continue expanding window with
j - Return
coas the maximum consecutive ones
Optimization:
- Only need one variable for zeros count
- Avoids recomputing sums or scanning subarrays repeatedly
Implementation (Java)
Dry Run Example
Input:
Execution:
Window [i, j] | Zeros z | Valid? | Max length co |
| [0,0] → [1] | 0 | Yes | 1 |
| [0,2] → [1,1,1] | 0 | Yes | 3 |
| [0,3] → [1,1,1,0] | 1 | Yes | 4 |
| [0,4] → [1,1,1,0,0] | 2 | Yes | 5 |
| [0,5] → [1,1,1,0,0,0] | 3 | No → shrink i | 5 |
| [3,8] → [0,0,1,1,1,1] | 2 | Yes | 6 |
Output:
Complexity Analysis
- Time Complexity: O(n) → Each element is visited at most twice (once when
jmoves, once whenimoves) - Space Complexity: O(1) → Only counters and pointers used
Edge Cases Considered
k = 0→ cannot flip any zeros, just count consecutive ones- Array with all 1’s → return full length
- Array with all 0’s → return
min(k, length) - Single element arrays → works correctly
Sliding Window Pattern Importance
This problem is a perfect example of the sliding window pattern:
- Use a window to track a condition (max zeros allowed)
- Expand and shrink dynamically based on constraints
- Efficiently computes maximum/minimum contiguous subarray lengths
It also demonstrates counting with limited violations – a key interview concept.
Conclusion
By tracking zeros with a sliding window, we convert a naive O(n²) problem into O(n) linear time.
Understanding this pattern allows you to solve:
- Max consecutive ones/zeros problems
- Longest substring/subarray with constraints
- Subarray problems with limited replacements or violations
Once mastered, this approach applies to many binary array and string problems efficiently.




