261. Max Consecutive Ones III

Given a binary array nums and an integer k, flip at most k 0's.

Return the maximum number of consecutive 1's after performing the flipping operation.

Example 1:

Input : nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0] , k = 3

Output : 10

Explanation : The maximum number of consecutive 1's are obtained only if we flip the 0's present at position 3, 4, 5 (0 base indexing).

The array after flipping becomes [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0].

The number of consecutive 1's is 10.

Example 2:

Input : nums = [0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1] , k = 3

Output : 9

Explanation : The underlines 1's are obtained by flipping 0's in the new array.

[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1].

The number of consecutive 1's is 9.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [1, 1, 0, 0, 1] , k = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 1
  • 0 <= k <= nums.length

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int longestOnes(vector<int>& nums, int k) {
//your code goes here
}
};
Test Case

Input:

K
Nums