VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-subarrays-m-odd-numbers/

⇱ Subarrays with k odd numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Subarrays with k odd numbers

Last Updated : 13 Aug, 2025

Given an integer array arr[] of size n, and a target integer k. Determine the number of contiguous subarrays (i.e., continuous segments of the array) that contain exactly k odd numbers.
A subarray is defined as a non-empty sequence of consecutive elements from the array.

Examples :

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] .

[Naive Approach] Generate All Possible Subarrays - O(n2) Time and O(1) Space

The idea is to generate all possible subarrays, and find the count of subarrays with number of odd elements equal to k.


Output
8

[Expected Approach] Using HashMap - O(n) Time and O(n) Space

The idea is to use a HashMap to track the frequency of prefix subarrays with a given count of odd numbers. For each element, maintain a running count of odd numbers seen so far. If the current odd count is at least k, then the number of subarrays ending at the current index with exactly k odd numbers is equal to prefix[odd - k]. Update the prefix count map as you iterate through the array.


Output
8

[Optimal Approach] Using Subarrays with (k- 1) Odds - O(n) Time and O(1) Space

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.

Step by Step Implementation

  • First, we create a function atMostX that counts how many subarrays have at most x odd numbers.
  • Inside this function, we use a sliding window with two pointers: one pointer i moves through the array, and the other pointer start marks the beginning of the current valid window.
  • We keep track of how many odd numbers are in the current window using a variable odd.
  • As we move i, if we find an odd number, we increase the odd count.
  • If the number of odd elements becomes more than x, we shrink the window from the left by moving start forward. If the element at start is odd, we decrease the odd count.
  • For every valid position of i, the number of subarrays ending at i with at most x odd numbers is (i - start + 1). We add this to our total answer.
  • Once we finish the loop, we return the total count from atMostX.
  • To count subarrays with exactly k odd numbers, we call atMostX twice — once with k, and once with k - 1.
  • The final result is the difference between these two values, because:
    => atMostX(k) counts all subarrays with up to k odd numbers.
    => atMostX(k - 1) counts those with fewer than k odd numbers.
    => Their difference gives us the count with exactly k odd numbers.

Output
8
Comment
Article Tags:
Article Tags: