> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/sliding-window/medium/1004.-max-consecutive-ones-iii.md).

# 1004. Max Consecutive Ones III

## Problem Statement

<br>

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

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
</strong><strong>Output: 6
</strong><strong>Explanation: [1,1,1,0,0,1,1,1,1,1,1]
</strong>Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
</code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
</strong><strong>Output: 10
</strong><strong>Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
</strong>Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
</code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length <= 105`
* `nums[i]` is either `0` or `1`.
* `0 <= k <= nums.length`

## Intuition

```
Approach:

Maintain a variable zero_count , That keeps track of zeros in the window
```

### Links

<https://leetcode.com/problems/max-consecutive-ones-iii/description/>

### Video Links

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int low=0, high=0;
        int ans=0;
        int zero_count=0;

        while(high<nums.size()){
            if(nums[high] == 0){
                zero_count++;
            }

            while(zero_count>k){
                if(nums[low] == 0)
                    zero_count--;

                low++;
            }

            ans = max(ans, high-low+1);
            high++;
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
