Fundamentals/Count Number of Nice Subarrays
← PrevNext →
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
Inputk = 1, arr = [2, 4, 6, 8, 10]
Output: 0
Explanation: There are no odd integers in arr, so there are no odd subarrays.
Constraints