#C4119. Count Subarrays with Exactly K Odd Numbers
Count Subarrays with Exactly K Odd Numbers
Count Subarrays with Exactly K Odd Numbers
You are given an array of n integers and an integer k. Your task is to count the number of contiguous subarrays (i.e., continuous segments of the array) that contain exactly k odd numbers.
The problem can be formalized as follows: Given an array \(nums\) and an integer \(k\), find the number of subarrays \(nums[i \ldots j]\) that satisfy:
[ \text{count}_{odd}(nums[i \ldots j]) = k ]
where \(\text{count}_{odd}(\cdot)\) counts the number of odd integers in the subarray.
Example:
Input: 5 3 1 1 2 1 1 Output: 2
In this example, there are exactly 2 contiguous subarrays with 3 odd numbers.
inputFormat
The first line contains two integers n and k, where n is the number of elements in the array, and k is the target number of odd numbers.
The second line contains n integers separated by spaces representing the elements of the array.
outputFormat
Output a single integer, representing the number of contiguous subarrays that contain exactly k odd numbers.
## sample5 3
1 1 2 1 1
2