 All Problems
Max Consecutive Ones III
medium
sliding window
arrays
google
amazon
microsoft

Given a binary array nums and an integer k, return the maximum number of consecutive 1s in the array if you can flip at most k 0s.

Example 1:

Input: 1 1 1 0 0 0 1 1 1 1 0
k: 2
Output: 6
Explanation: Flip positions 5 and 10.

Example 2:

Input: 0 0 1 1 0 0 1 1 1 0 1 1 0 0 0 1 1 1 1 0
k: 3
Output: 10

Constraints:

  • 1 ≤ nums.length ≤ 10⁵
  • nums[i] is 0 or 1
  • 0 ≤ k ≤ nums.length

Input format: First line: space-separated binary array. Second line: k.

Output format: Maximum consecutive 1s after at most k flips.

Run to check your code against the sample cases, or submit to run every case