Input : arr = [2, 5, 6, 9], k = 2
Output: 2
Explanation: There are 2 subarrays with 2 odds: [2, 5, 6, 9] and [5, 6, 9].
Input : arr = [2, 2, 5, 6, 9, 2, 11], k = 2
Output: 8
Explanation: There are 8 subarrays with 2 odds: [2, 2, 5, 6, 9], [2, 5, 6, 9], [5, 6, 9], [2, 2, 5, 6, 9, 2], [2, 5, 6, 9, 2], [5, 6, 9, 2], [6, 9, 2, 11] and [9, 2, 11] .
The idea is to HashMap to store the count of prefix subarrays with particular number of odd numbers, and then calculate the subarrays with count of odd numbers equal to k using this HashMap. To do so, create HashMap prefix[], where prefix[i] stores the count of prefix subarrays with number of odd elements equal to i. When the count of odd numbers exceeds or is equal to k, add the number of prefixes which has "(odd - k)" numbers to the answer.
The idea is to find the count of subarrays with at most k and k - 1 odd elements, then subtract the results to get the count of subarrays with exactly k odd elements. To find count of subarrays with at most x odd elements, run a loop from 0, and for each odd element, increment the counter count by 1. If count > k, then move the second pointer from 0, until count > k, store the length of subarray.