def f(A, k): # number of subarrays with at most k odd numbers
res = i = j = countOdd = 0
while j < len(A):
countOdd += A[j] % 2
while countOdd > k: # shrink window
countOdd -= A[i] % 2
i += 1
res += j - i + 1
j += 1
return res
foo = [1,1,2,1,1]
print(f(foo, 3) - f(foo, 2))