P19815 [GeeksforGeeks] Count subarray with k odds
普及
题目描述

You are given array nums of n elements and integer K, and count the number of subarrays that has k odd numbers.
Example1:
Input:
n = 5
nums = [1,1,2,1,1]
k = 3
Output:
2
Explanation:
There are 2 subarrays with k odds [1,1,2,1] and [1,2,1,1]
Example 2:
Input:
n=3
nums = [2,4,6]
k = 1
Output:
0
Your Task:
You don't have to read input or print anything. Your task is to complete the function countSubarray() which takes the integer n and array nums and integer K as input and returns the count of subarray having k odds number.
Expected Time Complexity: O(n)
Expected Space Complexity: O(n)
Constraint:
1 <= n <= 50000
1 <= nums <= 105
1 <= k <=n

输入格式

见题面

输出格式

见题面

样例输入
n = 5 nums = [1,1,2,1,1] k = 3
样例输出
2