#1248
Count Number of Nice Subarrays
pupil · 495 · lc medium +27 · verified · 74.8% accepted · 5,381 likes · top 85%
Description
You are given an integer array nums and an integer k. A contiguous subarray is called "nice" if it contains exactly k odd numbers.
Return the total count of nice subarrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There are no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Code
1
2
3