Max Consecutive Ones III

Medium

Description

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.

Examples

Input:nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output:6
Explanation:

Flip 2 zeros for longest 1s sequence.

Input:nums = [1], k = 2
Output:1
Explanation:

Edge case with a single-element array.

Input:nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0], k = 3
Output:9
Explanation:

It is possible to flip at most 3 zeros. The optimal subarray is [1,1,0,0,1,1,1,0,1,1] (indices 2-11), where flipping the 3 zeros at positions 4, 5, and 9 to get 9 consecutive ones.

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!