Count Number of Nice Subarrays
Problem Description
Given an array of integers nums and an integer k, return the number of nice subarrays.
A nice subarray has exactly k odd numbers in it.
Pattern: Prefix count / sliding window. Track count of odd numbers seen so far. For each position, if oddCount >= k, add count[oddCount - k] to result (number of previous positions where oddCount was exactly k less than now).
This is the same technique as "number of subarrays with sum = k" (LC 560).
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: [1,1,2,1] and [1,2,1,1] each have exactly 3 odd numbers.
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Example Test Cases
Example 1
Input: [[1,1,2,1,1],3]
Expected: 2
Example 2
Input: [[2,4,6],1]
Expected: 0
Example 3
Input: [[2,2,2,1,2,2,1,2,2,2],2]
Expected: 16
Run your code to see the results