Count Number of Nice Subarrays

We define a "nice" array as an array that contains exactly k odd numbers, where k is a user-defined number. In that case, given an array arr, find the number of "nice" continuous subarrays of that array (the array itself is its own subarray). Duplicate subarrays are counted as different subarrays as long as their index range is different.

Parameters

  • k: An integer required for the definition of a "nice" array.
  • arr: A list of integers representing the array.

Result

  • An integer representing the number of nice subarrays of arr.

Examples

Example 1

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

Output: 2

Explanation: The nice subarrays are [1, 1, 2, 1] and [1, 2, 1, 1].

Example 2

Input: k = 1, arr = [2, 4, 6, 8, 10]

Output: 0

Explanation: There are no odd integers in arr, so there are no odd subarrays.

Constraints

  • 1 <= k <= len(arr) <= 50000
  • 0 <= arr[i] <= 10^5

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro