97. Count Number of Nice Subarrays

MediumArrayArray

Given an array of integers nums and an integer k, return the number of nice subarrays. A subarray is nice if it has exactly k odd numbers among its elements.

Input: Integer array nums and integer k.

Output: Integer — count of subarrays with exactly k odd numbers.

Examples

Example 1
Input: [1,1,2,1,1],3
Output: 2
Explanation: [1,1,2,1] and [1,2,1,1] each contain 3 odd numbers.
Example 2
Input: [2,4,6],1
Output: 0
Explanation: No odd numbers → no nice subarrays.
Example 3
Input: [2,2,2,1,2,2,1,2,2,2],2
Output: 16
Explanation: 16 subarrays contain exactly 2 odd numbers.

Constraints

Asked by

MicrosoftGoogleOracleBloombergMetaAmazon
Solve this problem in the editor →