342. Count number of Nice subarrays

Given an array nums and an integer k. An array is called nice if and only if it contains k odd numbers. Find the number of nice subarrays in the given array nums.

A subarray is continuous part of the array.

Example 1:

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

Output : 2

Explanation : The subarrays with three odd numbers are

[1, 1, 2, 1]

[1, 2, 1, 1]

Example 2:

Input : nums = [4, 8, 2] , k = 1

Output : 0

Explanation : The array does not contain any odd number.

Now Your Turn!

Pick the correct output for the given input

Input : nums = [41, 3, 5] , k = 2

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 5*104
  • 1 <= nums[i] <= 105
  • 1 <= k <= nums.length

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int numberOfOddSubarrays(vector<int>& nums, int k) {
//your code goes here
}
};
Test Case

Input:

K
Nums