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.

&nbsp;
Example 1:

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

Example 2:

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


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	nums[i] is either 0 or 1.
	0 &lt;= k &lt;= nums.length


# TC : O(N)
# SC : O(1)
class Solution:
    def longestOnes(self, nums: List[int], k: int) -> int:
        z_count = 0 #count zeors in nums
        mx_ones = 0
        j = 0
        for i in range(len(nums)):
            if nums[i] == 0: 
                z_count+=1
            while z_count>k:#if zeros count cross k decrease count
                if nums[j] == 0:
                    z_count-=1
                j+=1
            print(i,j)
            mx_ones = max(mx_ones, i-j+1)
        return mx_ones
class Solution {
    public int longestOnes(int[] nums, int k) {
        int ans = 0;
        int j = -1;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                count++;
            }
            while (count > k) {
                j++;
                if (nums[j] == 0) {
                    count--;
                }
            }
            int len = i - j;
            if (len > ans) ans = len;
        }
        return ans;
    }
}
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        
        // max length of subarray with at most k zeroes 
    
        int i=0;
        int j=0;
        int cnt = 0;
        int ans = 0;

        int n = nums.size();

        while(j<n)
        {
            if(nums[j]==0)
                cnt++;

            if(cnt<=k)
            {
                ans = max(ans , j-i+1);
                j++;
            }
            else //cnt>k
            {
                while(cnt>k)
                {
                    if(nums[i]==0)
                        cnt--;
                    i++;
                }
                j++;
            }
        }
        return ans;
        
    }
};
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var longestOnes = function(nums, k) {
    let left = 0;
    let oneCount = 0;
    let maxLength = 0;
    
    for (let right = 0; right < nums.length; right ++) {
        if (nums[right]) {  // so if the element is a 1 since 0 is falsy
            oneCount++
        }
        
        if ((right - left + 1 - oneCount) > k) { // check if we've used all of our replacements
            if (nums[left]) { // start shrinking the window if its a 1 we subtract a count from oneCount
                oneCount--
            }
            left++
        }
        
        maxLength = Math.max(maxLength, right - left + 1); // update maxLength each iteration for largest window
    }
    
    return maxLength
};
Max Consecutive Ones III