Longest All-Ones Window After Limited Bit Flips
Arrays & HashingAuto-graded · 24 test casesLast updated
Tests the ability to reframe a flip problem as a sliding-window search and reason about amortized complexity.
You are given a list nums consisting only of 0s and 1s, along with an integer k that represents the maximum number of 0-to-1 flips you may perform.
Find the length of the longest contiguous segment of the array that can be turned into all 1s by flipping at most k of its elements from 0 to 1.
Return a single integer: the maximum possible length of such a segment.
Constraints:
0 <= len(nums) <= 10^5- Each element of
numsis either0or1. 0 <= k <= len(nums)
Edge-case rules:
- If the array is empty, return
0. - If
kis0, the answer is the length of the longest existing run of consecutive1s. - If
kis greater than or equal to the total number of0s in the array, the answer islen(nums). - If every element is
0, the answer ismin(k, len(nums)). - If every element is
1, the answer islen(nums).
Examples
Example 1
Input: longest_ones_after_flips([1, 0, 0, 1, 0, 1, 0, 0], 2)
Output: 4
Example 2
Input: longest_ones_after_flips([0, 0, 1, 1, 0, 0, 1, 1, 1, 0], 3)
Output: 8
Example 3
Input: longest_ones_after_flips([1, 1, 1, 1], 0)
Output: 4
Example 4
Input: longest_ones_after_flips([0, 0, 0], 5)
Output: 3
Hints
Hint 1
Reframe the problem: instead of tracking flips, find the longest contiguous segment that contains at most k zeros.
Hint 2
Use a variable-size sliding window with two pointers, keeping a count of zeros currently inside the window.
Hint 3
When the zero count exceeds k, advance the left pointer until the window is valid again. Each element is visited at most twice, giving O(n) time.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is a reframing. Instead of tracking which zeros to flip, recognize that flipping a 0 to 1 inside a window is equivalent to allowing the window to hold up to k zeros. The question then becomes: what is the longest contiguous subarray containing at most k zeros?
Use a variable-size sliding window defined by two pointers, left and right. Maintain a count of how many zeros are currently inside the window. Expand the window by advancing right one step at a time. Whenever the zero count exceeds k, shrink from the left by advancing left until the window is valid again (at most k zeros). After each step, record the current window size if it is larger than the best seen so far.
Why it works
Correctness rests on a monotonicity property. If a window [left, right] contains more than k zeros, then any window starting at an earlier index also contains more than k zeros, because it includes everything the current window does plus extra elements. This means the smallest valid left endpoint for each right never moves backward as right increases. The algorithm maintains exactly that smallest valid left for each right, so the window after shrinking is the longest valid window ending at that right. Taking the maximum across all positions of right yields the global answer.
Complexity
- Time: O(n). The
rightpointer advances n times total, and theleftpointer advances at most n times total across all iterations. The inner while loop is amortized O(1) per element. - Space: O(1). Only three integer variables are used beyond the input.
Reference solution
from typing import List
def longest_ones_after_flips(nums: List[int], k: int) -> int:
left = 0
zeros = 0
best = 0
for right, value in enumerate(nums):
if value == 0:
zeros += 1
while zeros > k:
if nums[left] == 0:
zeros -= 1
left += 1
best = max(best, right - left + 1)
return best