415. Max Consecutive Ones II

Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.

Example 1:

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

Output: 4

Explanation: 

If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones.

If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones.

The max number of consecutive ones is 4.

Example 2:

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

Output: 4

Explanation: 

If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones.

If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones.

The max number of consecutive ones is 4.

Now Your Turn!

Pick the correct output for the given input

Input : nums [1,1,0,0,1,1,0,0,1,1]

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
  • nums[i] is either 0 or 1.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums