30:00
Given an array of integers nums and an integer k, a continuous subarray is called nice if there are exactly k odd numbers in it.
nums
k
Return the number of nice subarrays.
1≤nums.length≤500001 \leq \text{nums.length} \leq 500001≤nums.length≤50000
1≤nums[i]≤1051 \leq \text{nums}[i] \leq 10^51≤nums[i]≤105
1≤k≤nums.length1 \leq k \leq \text{nums.length}1≤k≤nums.length
Run code to see results.
Only parity matters. Grow a window; once it holds 3 odd numbers, count how many starts can be dropped before the first odd.
odds in window: need 3
nums (orange = odd)
0
1
2
3
4
odds
-
gap
count
Given an integer array and k, count the contiguous subarrays that contain exactly k odd numbers.