Skip to content

Sliding Window — Max Consecutive Ones III ​

↗ LeetCode: Max Consecutive Ones III · Medium · pattern chapter →

Asked atMetaGoogleMicrosoftAmazon

Given a binary array nums and integer k, return the maximum length of a subarray containing only 1s after flipping at most k zeros.

Example 1 — nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 → 6 (flip the two zeros in the middle group; window [1,1,1,0,0,1,1,1,1] — no wait — final window [1,1,1,0,0,1,1,1]after flipping the middle 0s gives 6 in one run) **Example 2** —nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3→10**Example 3** —nums = [1,1,1,1], k = 0→4`

Constraints — 1 ≤ n ≤ 10⁵; nums[i] ∈ {0, 1}; 0 ≤ k ≤ n. Brute expands every window in O(n²) = 10¹⁰ ops at n=10⁵ (TLE). Sliding window with k-tolerance stays O(n) = 10⁵ ops.

Progressive Hints
Try to solve it yourself first. Reveal hints only when stuck — each hint is progressively more specific.
---
⏱️Interview timer00:00
Practice under interview pressure. Start when you begin reading; stop when you finish. Save best only when you had a full working solution.
Target for interview: Medium = 20-25 min · Hard = 30-40 min · Best time persists on this device only.

Approach 1 — Try every subarray ​

Intuition. For each [i, j], count zeros; if zeros ≤ k, track length.

java
int longestOnesBrute(int[] nums, int k) {
    int n = nums.length, best = 0;
    for (int i = 0; i < n; i++) {
        int zeros = 0;
        for (int j = i; j < n; j++) {
            if (nums[j] == 0) zeros++;
            if (zeros <= k) best = Math.max(best, j - i + 1);
            else break;
        }
    }
    return best;
}
Execution Trace
Brute — nums=[1,1,0,0,1,1], k=1
ij101102031415zeros = 1best = 3
Step 1 / 4one zero — allowed

Complexity — Time O(n²); Space O(1).


Approach 2 — Sliding window with zero counter ​

Insight from brute. Growing right never decreases the zero count. Once we exceed k, we must shrink left past a zero to reduce it.

java
int longestOnesSlide(int[] nums, int k) {
    int left = 0, zeros = 0, best = 0;
    for (int right = 0; right < nums.length; right++) {
        if (nums[right] == 0) zeros++;
        while (zeros > k) if (nums[left++] == 0) zeros--;
        best = Math.max(best, right - left + 1);
    }
    return best;
}
Execution Trace
Sliding — nums=[1,1,1,0,0,0,1,1,1,1,0], k=2
leftright10111203040516171819010zeros = 2best = 5
Step 1 / 4window contains 2 zeros ✓

Complexity — Time O(n) — each index enters/leaves window once; Space O(1).


Approach 3 — Non-shrinking window (interview polish) ​

Insight from sliding. For a maximum-length answer, we never need to shrink below best. We can keep the window size monotonic non-decreasing.

java
int longestOnes(int[] nums, int k) {
    int left = 0, zeros = 0;
    for (int right = 0; right < nums.length; right++) {
        if (nums[right] == 0) zeros++;
        if (zeros > k) { if (nums[left] == 0) zeros--; left++; }
    }
    return nums.length - left;
}
Execution Trace
Non-shrinking — same nums, k=2
leftright10111203040516171819010zeros = 3
Step 1 / 43rd zero → slide left by exactly 1

Complexity — Time O(n); Space O(1). Same asymptotics, one less inner loop.


Try it yourself ​

Complexity summary ​

ApproachTimeSpaceInterview grade
Every subarrayO(n²)O(1)baseline
Sliding window + zero counterO(n)O(1)expected optimum
Non-shrinking windowO(n)O(1)polish — one-pass, no inner while

When to use which ​

  • First pass — state brute, then jump to sliding-window with zero counter.
  • "Max length" only → prefer non-shrinking version.
  • "Return the window itself" → keep the shrinking version and track (bestL, bestLen).
  • "Flip 1s to 0s instead" → complementary problem — swap semantics.
✨AI Study CompanionBeta
Was this page helpful?

Released under MIT license.

💡 Quick shortcuts
H HintsR Run JavaA AI Help