You are given array nums of n length and an integer k .return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
Example:
Input:
n = 11
nums = [1,1,1,0,0,0,1,1,1,1,0]
k = 2
Output:
6
Explanation:
You can flip 2 0 and obtain  [1,1,1,0,0,1,1,1,1,1,1]
Your Task:
You don't have to read input or print anything. Your task is to complete the function longestOnes() which takes the integer n and array nums and integer K as input and returns the longest ones which can be obtained after k flip.
Expected Time Complexity: O(n)
Expected Space Complexity: O(1)
Constraint:
1<=n<=10^{5}
0<=nums<=1
0<=k<=n

Difficulty: MEDIUM
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3



class Solution:

    def longestOnes(self, n, a, k):

        # Code here

Solutions:
class Solution:

	def longestOnes(self, n, arr, k):
		(left, right) = (0, 0)
		zero_count = 0
		maxlen = 0
		while right < n:
			if arr[right] == 1:
				right += 1
			elif zero_count == k:
				zero_count -= 1 - arr[left]
				left += 1
			else:
				zero_count += 1
				right += 1
			maxlen = max(maxlen, right - left)
		return maxlen


class Solution:

	def longestOnes(self, n, a, k):
		i = 0
		j = 0
		max_ones = 0
		k_counter = 0
		while j < n:
			if a[j] == 0:
				k_counter += 1
			if k_counter > k:
				max_ones = max(max_ones, j - i)
				while k_counter > k:
					if a[i] == 0:
						k_counter -= 1
					i += 1
			j += 1
		max_ones = max(max_ones, j - i)
		return max_ones


class Solution:

	def longestOnes(self, n, a, k):
		left = 0
		zeros = 0
		max_ones = 0
		for right in range(len(a)):
			if a[right] == 0:
				zeros += 1
			while zeros > k:
				if a[left] == 0:
					zeros -= 1
				left += 1
			max_ones = max(max_ones, right - left + 1)
		return max_ones


class Solution:

	def longestOnes(self, n, a, k):
		zerocount = 0
		result = 0
		start = 0
		for i in range(n):
			if a[i] == 0:
				zerocount += 1
			while zerocount > k:
				if a[start] == 0:
					zerocount -= 1
				start += 1
			result = max(result, i - start + 1)
		return result


class Solution:

	def longestOnes(self, n, a, k):
		if a.count(0) <= k:
			return n
		(i, j) = (0, 0)
		ans = 0
		used = 0
		while i < n:
			if a[i] == 0:
				if used < k:
					used += 1
				else:
					ans = max(ans, i - j)
					while j <= i:
						if a[j] == 0:
							used -= 1
						j += 1
						if used < k:
							break
					i -= 1
			i += 1
		ans = max(ans, i - j)
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		ans = -1
		j = 0
		count = k
		for i in range(n):
			if a[i] == 0 and count == 0:
				while count == 0 and j < i:
					if a[j] == 0:
						count += 1
					j += 1
			if a[i] == 0:
				count -= 1
			ans = max(ans, i - j + 1)
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		start = 0
		zerocount = 0
		mx = 0
		for end in range(len(a)):
			if a[end] == 0:
				zerocount += 1
			while zerocount > k:
				if a[start] == 0:
					zerocount -= 1
				start += 1
			mx = max(mx, end - start + 1)
		return mx


class Solution:

	def longestOnes(self, n, nums, k):
		zero_count = 0
		res = 0
		i = 0
		c = 0
		for j in range(n):
			if nums[j] == 1:
				c += 1
			else:
				zero_count += 1
				c += 1
				while zero_count > k:
					if nums[i] == 0:
						zero_count -= 1
					i += 1
					c -= 1
			res = max(c, res)
		res = max(c, res)
		return res


class Solution:

	def longestOnes(self, n, a, k):
		(i, j, ans) = (0, 0, 0)
		state = 0
		while j < n:
			state += a[j] == 0
			while state > k:
				state -= a[i] == 0
				i += 1
			ans = max(ans, j - i + 1)
			j += 1
		return ans


class Solution:

	def longestOnes(self, n, arr, k):
		(l, r) = (0, 0)
		count = 0
		res = 0
		while r < n:
			count += 1 if arr[r] == 0 else 0
			while count > k:
				if arr[l] == 0:
					count -= 1
				l += 1
			res = max(res, r - l + 1)
			r += 1
		return res


class Solution:

	def longestOnes(self, n, a, k):
		i = j = 0
		res = 0
		while j < n:
			if a[j] == 0:
				k -= 1
			if k < 0:
				if a[j] == 0:
					while a[i] != 0:
						i += 1
					i += 1
			res = max(res, j - i + 1)
			j += 1
		return res


class Solution:

	def longestOnes(self, n, a, k):
		l = 0
		r = 0
		ans = 0
		s = []
		while r < n:
			if a[r] == 0:
				s.append(r)
			if len(s) > k:
				l = s.pop(0) + 1
			ans = max(ans, r - l + 1)
			r += 1
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		(s, e) = (0, 0)
		ans = 0
		while e < n:
			if a[e] == 0:
				k -= 1
			if k < 0:
				if a[s] == 0:
					k += 1
				s += 1
			e += 1
			ans = max(ans, e - s)
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		i = 0
		j = 0
		zero_count = 0
		ans = 0
		while i < n and j < n:
			if a[j] == 0:
				zero_count += 1
			while zero_count > k:
				if a[i] == 0:
					zero_count -= 1
				i += 1
			ans = max(ans, j - i + 1)
			j += 1
		return ans


class Solution:

	def longestOnes(self, n, arr, k):
		(r, l) = (0, 0)
		max_len = 0
		while r < n:
			if arr[r] == 1:
				r += 1
			elif arr[r] == 0 and k > 0:
				r += 1
				k -= 1
			elif arr[l] == 1:
				l += 1
			elif arr[l] == 0:
				l += 1
				k += 1
			max_len = max(max_len, r - l)
		return max_len


class Solution:

	def longestOnes(self, n, a, k):
		maxans = 0
		i = 0
		j = 0
		kcount = 0
		count = 0
		while j < n:
			if j < n and a[j] == 1:
				count += 1
			elif j < n and a[j] == 0:
				if kcount >= k:
					while i < n and a[i] != 0:
						i += 1
						count -= 1
					if count < 0:
						kcount = 0
						count = 0
					i += 1
				else:
					count += 1
					kcount += 1
			maxans = max(maxans, count)
			j += 1
		return maxans


class Solution:

	def longestOnes(self, n, a, k):
		countZero = 0
		right = 0
		left = 0
		while right < n:
			if a[right] == 0:
				countZero += 1
			if countZero > k:
				if a[left] == 0:
					countZero -= 1
				left += 1
			right += 1
		return right - left


class Solution:

	def longestOnes(self, n, nums, k):
		flip = k
		i = 0
		j = 0
		ans = -999999999
		while j < len(nums):
			if nums[j]:
				j += 1
			elif nums[j] == 0 and flip > 0:
				flip -= 1
				j += 1
			else:
				if nums[i] == 0:
					i += 1
				else:
					while nums[i]:
						i += 1
					i += 1
				j += 1
			if ans < j - i:
				ans = j - i
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		i = 0
		zero_count = 0
		max_one_count = 0
		for j in range(n):
			if a[j] == 0:
				zero_count = zero_count + 1
			if zero_count > k:
				if a[i] == 0:
					zero_count -= 1
				i += 1
			max_one_count = max(max_one_count, j - i + 1)
		return max_one_count


class Solution:

	def longestOnes(self, n, a, k):
		ans = 0
		l = 0
		for r in range(n):
			if a[r] == 0:
				k = k - 1
			while k < 0:
				if a[l] == 0:
					k = k + 1
				l = l + 1
			ans = max(ans, r - l + 1)
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		i = 0
		j = 0
		ans = 0
		cnt = 0
		kk = k
		while i < len(a) and j < len(a):
			if a[i] == 1:
				cnt += 1
				ans = max(ans, cnt)
				i += 1
			elif k > 0:
				cnt += 1
				k -= 1
				ans = max(ans, cnt)
				i += 1
			else:
				if a[j] == 1:
					cnt -= 1
				elif k < kk:
					k += 1
					cnt -= 1
				j += 1
				if j > i:
					i += 1
					j -= 1
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		c = j = len = 0
		for i in range(0, n):
			if a[i] == 0:
				c += 1
			while c > k:
				if a[j] == 0:
					c -= 1
				j += 1
			len = max(len, i - j + 1)
		return len


class Solution:

	def longestOnes(self, n, nums, k):
		max_ones = 0
		(left, right) = (0, 0)
		num_zeroes = 0
		while right < n:
			if nums[right] == 0:
				num_zeroes += 1
			while num_zeroes > k:
				if nums[left] == 0:
					num_zeroes -= 1
				left += 1
			max_ones = max(max_ones, right - left + 1)
			right += 1
		return max_ones


class Solution:

	def longestOnes(self, n, arr, k):
		st = 0
		ans = 0
		zerocounter = 0
		for ed in range(0, n):
			if arr[ed] == 0:
				zerocounter += 1
			while zerocounter > k:
				if arr[st] == 0:
					zerocounter -= 1
				st += 1
			ans = max(ans, ed - st + 1)
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		zero_count = 0
		l = 0
		max_one_count = 0
		for r in range(n):
			if a[r] == 0:
				zero_count = zero_count + 1
			if zero_count > k:
				if a[l] == 0:
					zero_count = zero_count - 1
				l = l + 1
			max_one_count = max(max_one_count, r - l + 1)
		return max_one_count


import sys

class Solution:

	def longestOnes(self, n, a, k):
		i = 0
		j = 0
		ans = -sys.maxsize
		numofzeroes = 0
		while j < n:
			if a[j] == 0:
				numofzeroes += 1
				while numofzeroes > k:
					if a[i] == 0:
						numofzeroes -= 1
					i += 1
			ans = max(ans, j - i + 1)
			j += 1
		return 0 if ans < 0 else ans


class Solution:

	def longestOnes(self, n, a, k):
		(numZeroes, l) = (0, 0)
		for r in range(n):
			if a[r] == 0:
				numZeroes += 1
			if numZeroes > k:
				if a[l] == 0:
					numZeroes -= 1
				l += 1
		return r - l + 1


class Solution:

	def longestOnes(self, n, a, k):
		zero_count = a.count(0)
		if k == zero_count:
			return n
		left = 0
		right = 0
		zero_count = 0
		max = 0
		for i in a:
			if i == 0:
				zero_count += 1
				if zero_count == k + 1:
					if right - left > max:
						max = right - left
					while a[left] != 0:
						left += 1
					left += 1
					zero_count -= 1
				right += 1
				if right - left > max:
					max = right - left
			else:
				right += 1
				if right - left > max:
					max = right - left
		return max


from collections import deque

class Solution:

	def longestOnes(self, n, a, k):
		stack = deque([])
		max_c = 0
		(i, j) = (0, 0)
		while j < n:
			if a[j] == 0:
				stack.append(j)
				if len(stack) > k:
					last_idx = stack.popleft()
					i = last_idx + 1
			max_c = max(max_c, j - i + 1)
			j += 1
		return max_c


class Solution:

	def longestOnes(self, n, a, k):
		l = 0
		r = 0
		temp = k
		c = 0
		while r < n:
			if a[r] == 1:
				c = max(c, r - l + 1)
				r += 1
			elif k != 0:
				c = max(c, r - l + 1)
				r += 1
				k -= 1
			else:
				k = temp
				l += 1
				r = l
		return c


class Solution:

	def longestOnes(self, n, nums, k):
		(i, j, zC, ans) = (0, 0, 0, float('-inf'))
		while j < len(nums):
			if nums[j] == 0:
				zC += 1
			while zC > k:
				if nums[i] == 0:
					zC -= 1
				i += 1
			ans = max(ans, j - i + 1)
			j += 1
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		i = 0
		j = 0
		count = 0
		ans = float('-inf')
		while j < n:
			if a[j] == 0:
				count += 1
			while count > k:
				if a[i] == 0:
					count -= 1
				i += 1
			ans = max(ans, j - i + 1)
			j += 1
		return ans


import sys

class Solution:

	def longestOnes(self, n, a, k):
		start = 0
		end = 0
		zeroCount = 0
		maxCount = 0
		for end in range(n):
			if a[end] == 0:
				zeroCount += 1
			while zeroCount > k:
				if a[start] == 0:
					zeroCount -= 1
				start += 1
			maxCount = max(maxCount, end - start + 1)
		return maxCount


class Solution:

	def longestOnes(self, n, nums, k):
		l = 0
		for r in range(len(nums)):
			if nums[r] == 0:
				k -= 1
			if k < 0:
				if nums[l] == 0:
					k += 1
				l += 1
		return r - l + 1


class Solution:

	def longestOnes(self, n, a, k):
		start = 0
		end = 0
		countzeros = 0
		maxv = 0
		while end < len(a):
			if a[end] == 0:
				countzeros += 1
			while countzeros > k:
				if a[start] == 0:
					countzeros -= 1
				start += 1
			maxv = max(maxv, end - start + 1)
			end += 1
		return maxv


class Solution:

	def longestOnes(self, n, a, k):
		s = 0
		e = 0
		zero = 0
		maxi = 0
		while s < n:
			if a[s] == 0:
				zero += 1
			while zero > k:
				if a[e] == 0:
					zero -= 1
				e += 1
			maxi = max(maxi, s - e + 1)
			s += 1
		return maxi


class Solution:

	def longestOnes(self, n, nums, k):
		l = 0
		r = 0
		n = len(nums)
		count = 0
		while r < n:
			if nums[r] == 0:
				k -= 1
			while k < 0:
				if nums[l] == 0:
					k += 1
				l += 1
			count = max(count, r - l + 1)
			r += 1
		return count


class Solution:

	def longestOnes(self, n, arr, k):
		ma = float('-inf')
		(i, j) = (0, 0)
		c0 = 0
		while j < n:
			if arr[j] == 0:
				c0 += 1
			if c0 < k:
				j += 1
			elif c0 == k:
				ma = max(ma, j - i + 1)
				j += 1
			else:
				while c0 > k:
					if arr[i] == 0:
						c0 -= 1
					i += 1
				j += 1
		if ma == float('-inf'):
			return n
		return ma


class Solution:

	def longestOnes(self, n, a, k):
		(l, r) = (0, 0)
		(zero, ones) = (0, 0)
		ans = 0
		while r < n:
			if a[r] == 0:
				zero += 1
			else:
				ones += 1
			while l < r and zero > k:
				if a[l] == 0:
					zero -= 1
				else:
					ones -= 1
				l += 1
			ans = max(ans, r - l + 1)
			r += 1
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		(start, end, zeroes, ans) = (0, 0, 0, 0)
		while end < n:
			if zeroes <= k:
				if a[end] == 0:
					zeroes += 1
				end += 1
				if end == n:
					if zeroes > k:
						ans = max(ans, end - start - 1)
					else:
						ans = max(ans, end - start)
			else:
				ans = max(ans, end - start - 1)
				if a[start] == 0:
					zeroes -= 1
				start += 1
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		originalk = k
		j = 0
		i = 0
		ans = 0
		zc = 0
		while j < n:
			if a[j] == 0:
				zc = zc + 1
			if zc > k:
				while zc > k:
					if a[i] == 0:
						zc = zc - 1
					i = i + 1
			ans = max(ans, j - i + 1)
			j = j + 1
		return ans


class Solution:

	def longestOnes(self, n, arr, k):
		ans = 0
		start = end = 0
		count0 = count1 = 0
		while end < n:
			if arr[end] == 0:
				count0 += 1
			if count0 > k:
				if arr[start] == 0:
					count0 -= 1
				start += 1
			else:
				ans = max(ans, end - start + 1)
			end += 1
		return ans


class Solution:

	def longestOnes(self, n, nums, k):
		mx = 0
		start = 0
		currant_zero = 0
		for end in range(len(nums)):
			if nums[end] == 0:
				currant_zero += 1
			while currant_zero > k:
				if nums[start] == 0:
					currant_zero -= 1
				start += 1
			mx = max(mx, end - start + 1)
		return mx


class Solution:

	def longestOnes(self, n, arr, k):
		(length, max_l, l) = (0, 0, 0)
		for r in range(n):
			if arr[r] == 0:
				k += -1
			while k < 0:
				if arr[l] == 0:
					k += 1
				l += 1
			length = r - l + 1
			if length > max_l:
				max_l = length
		return max_l


class Solution:

	def longestOnes(self, n, a, k):
		(left, right) = (0, 0)
		mx = 0
		cnt = 0
		while right < n:
			if a[right] == 0:
				cnt += 1
			while cnt > k:
				if a[left] == 0:
					cnt -= 1
				left += 1
			mx = max(mx, right - left + 1)
			right += 1
		return mx


class Solution:

	def longestOnes(self, n, a, k):
		(left, right) = (0, 0)
		for right in range(n):
			if a[right] == 0:
				k -= 1
			if k < 0:
				if a[left] == 0:
					k += 1
				left += 1
		return right - left + 1


class Solution:

	def longestOnes(self, n, a, k):
		(l, r, z) = (0, 0, 0)
		res = 0
		while r < n:
			if a[r] == 0:
				z += 1
			while z > k:
				if a[l] == 0:
					z -= 1
				l += 1
			res = max(res, r - l + 1)
			r += 1
		return res


class Solution:

	def longestOnes(self, n, a, k):
		left = right = zeros = ans = 0
		while right < n:
			if a[right] == 0:
				zeros += 1
			while zeros > k:
				if a[left] == 0:
					zeros -= 1
				left += 1
			ans = max(ans, right - left + 1)
			right += 1
		return ans


class Solution:

	def longestOnes(self, n, arr, m):
		(i, j, ans, zerocount) = (0, 0, 0, 0)
		n = len(arr)
		while j < n:
			if arr[j] == 0:
				zerocount += 1
			if zerocount <= m:
				ans = max(ans, j - i + 1)
				j += 1
			elif zerocount > m:
				while zerocount > m:
					if arr[i] == 0:
						zerocount -= 1
					i += 1
				j += 1
		return ans


class Solution:

	def longestOnes(self, n, a, k):
		(s, e, z) = (0, 0, 0)
		res = 0
		for e in range(n):
			if a[e] == 0:
				z += 1
			while z > k:
				if a[s] == 0:
					z -= 1
				s += 1
			res = max(res, e - s + 1)
		return res


class Solution:

	def longestOnes(self, n, a, k):
		ans = 0
		temp = 0
		zCount = 0
		(i, j) = (0, 0)
		while j < n:
			if a[j] == 0:
				temp += 1
				zCount += 1
			if temp == k:
				if ans < j - i + 1:
					ans = j - i + 1
			elif temp > k:
				while temp != k:
					if a[i] == 0:
						temp -= 1
					i += 1
			j += 1
		return n if zCount <= k else ans


class Solution:

	def longestOnes(self, n, arr, k):
		(l, r) = (0, 0)
		zero_count = 0
		res = 0
		while r < n:
			if zero_count <= k:
				if arr[r] == 0:
					zero_count += 1
				r += 1
			if zero_count > k:
				if arr[l] == 0:
					zero_count -= 1
				l += 1
			if r - l > res and zero_count <= k:
				res = r - l
		return res


class Solution:

	def longestOnes(self, n, a, m):
		i = 0
		j = -1
		count = 0
		ans = 0
		for i in range(len(a)):
			if a[i] == 0:
				count += 1
			while count > m:
				j += 1
				if a[j] == 0:
					count -= 1
			if i - j > ans:
				ans = i - j
		return ans


class Solution:

	def longestOnes(self, n, arr, m):
		i = 0
		j = 0
		d = -1
		c = 0
		while j < n:
			if arr[j] == 0:
				c += 1
			while c > m:
				if arr[i] == 0:
					c -= 1
				i += 1
			e = j - i + 1
			if e > d:
				d = e
			j += 1
		return d


class Solution:

	def longestOnes(self, n, arr, k):
		i = 0
		j = 0
		for j in range(n):
			if arr[j] == 0:
				k -= 1
			if k < 0:
				if arr[i] == 0:
					k += 1
				i += 1
		return j - i + 1


class Solution:

	def longestOnes(self, n, a, k):
		maxlen = 0
		i = 0
		for j in range(n):
			if a[j] == 0 and k != 0:
				k -= 1
			elif a[j] == 0 and k == 0:
				while k == 0:
					if a[i] == 0:
						k += 1
					i += 1
				k -= 1
			maxlen = max(maxlen, j - i + 1)
		return maxlen


class Solution:

	def longestOnes(self, n, a, k):
		s = 0
		z = 0
		m = 0
		for end in range(len(a)):
			if a[end] == 0:
				z += 1
			while z > k:
				if a[s] == 0:
					z -= 1
				s += 1
			m = max(m, end - s + 1)
		return m


class Solution:

	def longestOnes(self, n, nums, k):
		n = len(nums)
		start = 0
		maxConsecutive1 = 0
		zeros = 0
		for end in range(n):
			if nums[end] == 0:
				zeros += 1
			while zeros > k:
				if nums[start] == 0:
					zeros -= 1
				start += 1
			maxConsecutive1 = max(maxConsecutive1, end - start + 1)
		return maxConsecutive1


class Solution:

	def longestOnes(self, n, a, k):
		left = 0
		presum = 0
		result = 0
		for right in range(len(a)):
			presum += a[right]
			if right - left + 1 - presum > k:
				presum -= a[left]
				left += 1
			result = max(result, right - left + 1)
		return result