Count Number of Nice Subarrays

Given an integer array nums and a non-negative integer k, return the number of non-empty contiguous subarrays that contain exactly k odd numbers. A number is…

Given an integer array `nums` and a non-negative integer `k`, return the number of non-empty contiguous subarrays that contain exactly `k` odd numbers. A number is odd when it is not divisible by 2. Negative odd numbers count as odd.

- `1 <= nums.length <= 5 * 10^4` - `-10^9 <= nums[i] <= 10^9` - `0 <= k <= nums.length` - The interactive visualizer accepts at most 12 values; longer valid arrays are execution-only.

Example 1: input nums = [1,1,2,1,1], k = 3; output 2. The ranges [0..3] and [1..4] each contain exactly three odd numbers.

Example 2: input nums = [2,4,6], k = 1; output 0. No range can contain one odd number because every value is even.

Example 3: input nums = [2,4,6], k = 0; output 6. Every non-empty subarray contains exactly zero odd numbers.

Category: Sliding Window. Topic: sliding-window. Difficulty: Medium. Patterns: sliding-window.