Max Consecutive Ones III - Problem

Imagine you have a binary array filled with 0s and 1s, and you're given a special power: you can flip at most k zeros into ones. Your mission is to find the maximum number of consecutive 1s you can create using this power strategically.

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

Example: If you have [1,1,1,0,0,0,1,1,1,1,0] and k = 2, you could flip the zeros at positions 3 and 4 to get [1,1,1,1,1,0,1,1,1,1,0], creating 5 consecutive ones. But there's an even better strategy!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
โ€บ Output: 6
๐Ÿ’ก Note: We can flip the zeros at indices 4 and 5 to get [1,1,1,0,0,1,1,1,1,1,0], creating a sequence of 6 consecutive ones from index 5 to 10. Alternatively, flipping indices 3 and 4 gives us [1,1,1,1,1,0,1,1,1,1,0] with 5 consecutive ones, but 6 is better.
example_2.py โ€” All Zeros Case
$ Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
โ€บ Output: 10
๐Ÿ’ก Note: We can flip 3 zeros strategically to create the longest possible sequence. By flipping zeros at positions that connect existing sequences of ones, we can achieve a maximum length of 10.
example_3.py โ€” No Flips Needed
$ Input: nums = [1,1,1,1], k = 0
โ€บ Output: 4
๐Ÿ’ก Note: The array already contains all ones, so no flips are needed. The maximum consecutive ones is the entire array length of 4.

Visualization

Tap to expand
๐ŸŒ‰ Bridge Building VisualizationLandWaterWaterLandLandWaterArray: [1, 0, 0, 1, 1, 0] with k=2 bridge segmentsBridge Segment 1Bridge Segment 2Measuring Tape: Length = 6โœ… Current span: 6 units (using 2 bridge segments)๐Ÿ“ This is our maximum achievable bridge length!Sliding Window StrategyExpand right until bridge budget exceededContract left until within budget again
Understanding the Visualization
1
Start with a measuring tape
Place your measuring tape at the beginning (left pointer) and start extending it (right pointer)
2
Count bridge segments used
As you encounter water gaps (0s), use bridge segments. Keep track of how many you've used
3
Retract when over budget
When you've used more than k segments, pull back the left end of your tape until you're within budget
4
Record maximum span
Throughout this process, keep track of the longest valid span you've measured
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window technique allows us to find the optimal solution in O(n) time by maintaining a valid window that never contains more than k zeros, expanding and contracting as needed.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Binary search runs in O(log n) iterations, each checking O(n) subarrays

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using variables for binary search bounds and counting

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • nums[i] is either 0 or 1
  • 0 โ‰ค k โ‰ค nums.length
  • Follow up: Can you solve this in O(n) time and O(1) space?
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 25
58.9K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen