Introduction
The Maximum Consecutive Ones III problem asks us to find the longest sequence of 1s that can be obtained by flipping at most k zeros.
Given a binary array and an integer k, we can change at most k zeros into 1s.
The goal is to find the maximum length of consecutive 1s after these changes.
This problem helps in understanding:
- Sliding Window
- Two Pointer Technique
- Window expansion and shrinking
- Counting elements inside a window
Problem Statement
Given a binary array nums and an integer k, return the maximum number of consecutive 1s in the array after flipping at most k zeros.
Example
Input:
nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]
k = 2
Output:
6
Explanation:
By flipping two zeros, we can obtain a windowcontaining 6 consecutive 1s.
Input:nums = [0, 0, 1, 1, 0, 0, 1, 1, 1, 0]
k = 2
Output:
6
Explanation:
At most two zeros can be flipped to 1.
The longest valid window has length 6.
Constraints
- 1 ≤ nums.length ≤ 10⁵
- nums[i] is either 0 or 1
- 0 ≤ k ≤ nums.length
Approach 1: Brute Force
Explanation
The brute force approach considers every possible subarray.
For each starting position, expand the subarray and count the number of zeros.
If the number of zeros is at most k, the current window is valid.
If the number of zeros becomes greater than k, stop expanding that window.
Steps
- Start from every index.
- Expand the window to the right.
- Count the number of zeros.
- If zeros are at most k, update the maximum length.
- Stop when zeros become greater than k.
Dry Run
Input:nums = [1, 1, 0, 0, 1]
k = 1
Start at index 0:
[1] → zeros = 0 → length = 1
[1, 1] → zeros = 0 → length = 2
[1, 1, 0] → zeros = 1 → length = 3
[1, 1, 0, 0] → zeros = 2 → invalid
Maximum length:
3
Final Result:
3
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)
Every possible starting position can expand across the array.
Space Complexity: O(1)
Only a few variables are used.
Approach 2: Optimized Solution
Explanation
The optimized approach uses the Sliding Window Technique.
We maintain a window using two pointers:
- left points to the beginning of the window.
- right moves through the array.
We count the number of zeros inside the window.
If the number of zeros becomes greater than k, move left forward until the window becomes valid again.
This allows us to process each element efficiently.
Steps
- Initialize left = 0, zeros = 0, and maxLength = 0.
- Move right through the array.
- If the current element is 0, increment zeros.
- If zeros > k, move left forward.
- Whenever a zero leaves the window, decrease zeros.
- Update maxLength using the current window size.
- Return maxLength.
Dry Run
Input:nums = [1, 1, 0, 0, 1]
k = 1
right = 0:
window = [1]
zeros = 0
length = 1
right = 1:
window = [1, 1]
zeros = 0
length = 2
right = 2:
window = [1, 1, 0]
zeros = 1
length = 3
right = 3:
window = [1, 1, 0, 0]
zeros = 2
zeros > k
Move left until one zero is removed.
Valid window:
[1, 0, 0, 1] → still 2 zeros
Move left again:
[0, 0, 1] → still 2 zeros
Move left again:
[0, 1] → zeros = 1
length = 2
right = 4:
window = [0, 1]
zeros = 1
length = 2
Final Result:
3
Optimized Code
Complexity Analysis
Time Complexity: O(n)
Each element enters and leaves the sliding window at most once.
Space Complexity: O(1)
Only a constant number of variables are used.
Edge Cases
- k = 0: No zeros can be flipped.
- k equals the array length: All elements can potentially be converted to 1.
- All elements are 1: The answer is the array length.
- All elements are 0: The answer depends on k.
- Single element: The answer is either 0 or 1 depending on the value and k.
Why This Problem is Important
This problem is important for understanding:
- Sliding Window
- Two Pointer Technique
- Dynamic window size
- Maintaining window conditions
- Linear-time optimization
Real-World Applications
Sliding window techniques can be used in:
- Network monitoring
- Continuous data analysis
- Signal processing
- Log analysis
- Performance monitoring
Common Mistakes
- Forgetting to decrease the zero count when moving left.
- Updating the maximum before making the window valid.
- Using nested loops when a sliding window can be used.
- Moving left without checking whether the removed element is zero.
- Using the wrong window length formula.
Interview Tips
The key observation is:
The window can contain at most k zeros.
Whenever the window contains more than k zeros, move the left pointer until the condition becomes valid again.
The window length is:
right − left + 1
Related Questions
- Maximum Consecutive Ones
- Minimum Size Subarray Sum
- Maximum Average Subarray I
- Number of Sub-arrays of Size K and Average ≥ Threshold
- Longest Subarray with At Most K Distinct Elements
Final Takeaway
The Maximum Consecutive Ones III problem is a classic Sliding Window problem.
The brute force approach checks possible subarrays, while the optimized approach maintains a valid window containing at most k zeros, achieving O(n) time and O(1) extra space.