All problems

Count Number of Nice Subarrays

Medium · prefix sum

You are given a list nums of positive integers and an integer k.

A run is a block of one or more neighbouring numbers in the list. Return how many runs contain exactly k odd numbers. The even numbers in a run do not matter.

Runs are told apart by where they sit in the list, so two runs with the same values in different places each count. If there are none, return 0.

Examples

Example 1

Input: nums = [1, 2, 3], k = 2

Output: 1

Only the whole list holds two odd numbers (1 and 3).

Example 2

Input: nums = [2, 1, 2], k = 1

Output: 4

The runs [1], [2, 1], [1, 2] and [2, 1, 2] each hold exactly one odd number.

Example 3

Input: nums = [2, 4, 6], k = 1

Output: 0

There are no odd numbers at all, so no run qualifies.

Constraints