Instead of keeping track of maxOnesCount, I found it easier to keep track of both counts. I thought the code was a lot more simple:
def length_of_longest_substring(arr, k):
start, max_len = 0, 0
freq = {0: 0, 1: 0}
for end in range(len(arr)):
freq[arr[end]] += 1
while (freq[0] > k):
freq[arr[start]] -= 1
start += 1
max_len = max(max_len, end-start+1)
return max_len