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 window
containing 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

  1. Start from every index.
  2. Expand the window to the right.
  3. Count the number of zeros.
  4. If zeros are at most k, update the maximum length.
  5. 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

  1. Initialize left = 0, zeros = 0, and maxLength = 0.
  2. Move right through the array.
  3. If the current element is 0, increment zeros.
  4. If zeros > k, move left forward.
  5. Whenever a zero leaves the window, decrease zeros.
  6. Update maxLength using the current window size.
  7. 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

  1. k = 0: No zeros can be flipped.
  2. k equals the array length: All elements can potentially be converted to 1.
  3. All elements are 1: The answer is the array length.
  4. All elements are 0: The answer depends on k.
  5. 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:

  1. Sliding Window
  2. Two Pointer Technique
  3. Dynamic window size
  4. Maintaining window conditions
  5. Linear-time optimization

Real-World Applications

Sliding window techniques can be used in:

  1. Network monitoring
  2. Continuous data analysis
  3. Signal processing
  4. Log analysis
  5. Performance monitoring

Common Mistakes

  1. Forgetting to decrease the zero count when moving left.
  2. Updating the maximum before making the window valid.
  3. Using nested loops when a sliding window can be used.
  4. Moving left without checking whether the removed element is zero.
  5. 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

  1. Maximum Consecutive Ones
  2. Minimum Size Subarray Sum
  3. Maximum Average Subarray I
  4. Number of Sub-arrays of Size K and Average ≥ Threshold
  5. 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.