Quant Memo
Coding/●●●●●

Count subarrays with exactly k odd numbers

Given an integer array nums and a target k, count the number of contiguous subarrays that contain exactly k odd numbers.

nums = [1, 1, 2, 1, 1], k = 3
-> 2          # [1,1,2,1] and [1,2,1,1]

Target O(n)O(n) time.

Show a hint

Replace each number by 1 if it is odd and 0 if it is even. What does "exactly k odd numbers" become?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions