Count Number of Nice Subarrays

Medium

Description

Given an array of integers nums and an integer k, return the number of nice subarrays. A nice subarray is a subarray with exactly k odd numbers.

Examples

Input:nums = [1,1,2,1,1], k = 3
Output:2
Explanation:

[1,1,2,1] and [1,2,1,1] have 3 odds.

Input:nums = [2,4,6,8], k = 1
Output:0
Explanation:

The array contains only even numbers [2,4,6,8], so there are no odd numbers. Since the requirement is exactly 1 odd number and there are none, no subarrays can be nice.

Input:nums = [3,5,7,2,4,9,11], k = 2
Output:9
Explanation:

The odd numbers are at indices 0,1,2,5,6. Subarrays with exactly 2 odds: [3,5], [3,5,7], [3,5,7,2], [3,5,7,2,4], [5,7], [5,7,2], [5,7,2,4], [2,4,9,11], and [4,9,11]. Total: 9 nice subarrays.

Constraints

  • 1 ≤ nums.length ≤ 50000
  • 1 ≤ nums[i] ≤ 10^5

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!