MediumArrayBinary SearchSliding WindowPrefix SumUpdated Sep 2026

1004. Max Consecutive Ones III

Asked at Meta, DE Shaw

Difficulty

Medium

Time

O(n)

Space

O(1)

Pattern

Array

Companies

2

Problem

You get a binary array nums (only 0s and 1s) and an integer k. You may change at most k of the 0s into 1s. After doing that, some run of 1s becomes longer.

Return the length of the longest block of consecutive 1s you can create. The block must be a contiguous subarray — the elements must sit next to each other. You do not have to use all k flips; using fewer is allowed.

Think of it this way: find the longest window that contains at most k zeros. Every zero inside that window can be flipped, so the whole window becomes 1s.

Flip at most k zeros, then measure the longest run

nums11100011110012345678910The two orange zeros are the ones we flip (k = 2).after flipslength 611111101111012345678910Indices 0 to 5 are now all 1s, giving a run of length 6.
With k = 2 we flip two zeros and the best block of 1s has length 6.

Constraints

  • ●1 <= nums.length <= 10^5
  • ●nums[i] is either 0 or 1
  • ●0 <= k <= nums.length

Examples

Example 1
Inputnums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output6
WhyFlip the two zeros at indices 3 and 4. The window from index 0 to index 5 then reads 1,1,1,1,1,1 — six 1s in a row. No window of length 7 has only 2 zeros, so 6 is the best.
Example 2
Inputnums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output10
WhyFlip the three zeros at indices 4, 5 and 9. The window from index 2 to index 11 becomes ten 1s in a row. Any longer window would need more than 3 flips.

Asked At

2 companies
CompanyDifficulty
MetaMediumView all Meta questions →
DE ShawMediumView all DE Shaw questions →

How to Think About It

1

Brute force: for each starting index, try flipping up to k zeros and count the longest run.

2

Use a sliding window that tracks the count of zeros in the current window.

3

Expand the right pointer and shrink the left pointer when zeros exceed k.

4

Binary search on prefix sums: for each index i, find the farthest j where sum[j] - sum[i] <= k.

5

Optimal O(n) sliding window with O(1) space by maintaining a zero counter.

Optimal Approach

Use a sliding window approach. Maintain left and right pointers. Expand right and count zeros encountered. When zeros exceed k, move left pointer forward while decrementing zeros. Track the maximum window size at each step. This runs in O(n) time with O(1) space. The window [left, right] always contains at most k zeros, and the answer is the maximum window size.

Sliding window with a zero counter

numszeros = 211100011110012345678910leftrightThe window [0..4] has exactly 2 zeros, so it is still valid.numszeros = 211100011110012345678910leftrightRight hits a third zero, so left moves past the first zero and the window isvalid again.numsbest = 611100011110012345678910leftrightThe widest valid window seen is [0..5], length 6 — that is the answer.if nums[right] == 0: zeros += 1while zeros > k: shrink leftbest = max(best, right - left + 1)
Grow right freely; when the window holds more than k zeros, push left forward until it holds k again.
ComplexityTime O(n)Space O(1)Each pointer moves forward at most n times, and we only keep a zero counter and two indices.

What Trips People Up in Real Interviews

1

Clarify if the array contains only 0s and 1s.

2

Ask whether k zeros can be flipped or exactly k must be flipped.

3

Mention that the sliding window approach is preferred over binary search in interviews.

4

Edge cases: k equals array length (return n), k is 0 (count existing runs), no zeros.

5

Walk through an example like [1,1,0,0,1,1,1,0,1] with k=2 step by step.

Solution Code

O(n) time
def longestOnes(nums, k):
    left = 0
    zeros = 0
    max_len = 0
    for right in range(len(nums)):
        if nums[right] == 0:
            zeros += 1
        while zeros > k:
            if nums[left] == 0:
                zeros -= 1
            left += 1
        max_len = max(max_len, right - left + 1)
    return max_len

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Max Consecutive Ones III problem?

Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's. This problem tests your ability to optimize a sliding window using prefix sums or binary search on the number of flips.

How do you solve Max Consecutive Ones III?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Max Consecutive Ones III?

Max Consecutive Ones III is asked at Meta, DE Shaw. It is a medium difficulty problem.

What are common mistakes on Max Consecutive Ones III?
  • Clarify if the array contains only 0s and 1s.
  • Ask whether k zeros can be flipped or exactly k must be flipped.
  • Mention that the sliding window approach is preferred over binary search in interviews.
  • Edge cases: k equals array length (return n), k is 0 (count existing runs), no zeros.
  • Walk through an example like [1,1,0,0,1,1,1,0,1] with k=2 step by step.