# Number of subarrays with m odd numbers

Given an array of n elements and an integer m, we need to write a program to find the number of contiguous subarrays in the array which contains exactly m odd numbers.

Examples :

```Input : arr = {2, 5, 6, 9},  m = 2
Output : 2
Explanation: subarrays are [2, 5, 6, 9]
and [5, 6, 9]

Input : arr = {2, 2, 5, 6, 9, 2, 11},  m = 2
Output : 8
Explanation: subarrays are [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]
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The naive approach is to generate all possible subarrays and simultaneously checking for the subarrays with m odd numbers.

Below is the implementation of the above approach:

## C++

 `// CPP program to count the ` `// Number of subarrays with  ` `// m odd numbers ` `#include ` `using` `namespace` `std; ` ` `  `// fucntion that returns  ` `// the count of subarrays ` `// with m odd numbers ` `int` `countSubarrays(``int` `a[],  ` `                   ``int` `n, ``int` `m) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``// traverse for all  ` `    ``// possible subarrays ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``int` `odd = 0; ` `        ``for` `(``int` `j = i; j < n; j++) ` `        ``{ ` `            ``if` `(a[j] % 2) ` `                ``odd++; ` ` `  `            ``// if count of odd numbers in ` `            ``// subarray is m ` `            ``if` `(odd == m) ` `                ``count++; ` `        ``} ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = { 2, 2, 5, 6, 9, 2, 11 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``int` `m = 2; ` ` `  `    ``cout << countSubarrays(a, n, m); ` `    ``return` `0; ` `} `

## Java

 `// Java program to count the number of  ` `// subarrays with m odd numbers ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// function that returns the count of ` `    ``// subarrays with m odd numbers    ` `       ``static` `int` `countSubarrays(``int` `a[], ` `                              ``int` `n, ``int` `m) ` `    ``{ ` `         `  `        ``int` `count = ``0``; ` ` `  `        ``// traverse for all possible ` `        ``// subarrays ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``int` `odd = ``0``; ` `            ``for` `(``int` `j = i; j < n; j++)  ` `            ``{ ` `                ``if` `(a[j] % ``2` `==``0``) ` `                    ``odd++; ` `     `  `                ``// if count of odd numbers ` `                ``// in subarray is m ` `                ``if` `(odd == m) ` `                    ``count++; ` `            ``} ` `        ``} ` `         `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `a[] = { ``2``, ``2``, ``5``, ``6``, ``9``, ``2``, ``11` `}; ` `        ``int` `n = a.length; ` `        ``int` `m = ``2``; ` `         `  `        ``System.out.println(countSubarrays(a, n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by akash1295. `

## Python3

 `# Python3 program to count the ` `# Number of subarrays with  ` `# m odd numbers ` ` `  `# function that returns the count  ` `# of subarrays with m odd numbers ` `def` `countSubarrays(a, n, m): ` `    ``count ``=` `0``; ` ` `  `    ``# traverse for all ` `    ``# possible subarrays ` `    ``for` `i ``in` `range``(n):  ` `        ``odd ``=` `0``; ` `        ``for` `j ``in` `range``(i, n):  ` `            ``if` `(a[j] ``%` `2``): ` `                ``odd ``+``=` `1``; ` ` `  `            ``# if count of odd numbers  ` `            ``# in subarray is m ` `            ``if` `(odd ``=``=` `m): ` `                ``count ``+``=` `1``; ` `    ``return` `count; ` ` `  `# Driver Code ` `a ``=` `[ ``2``, ``2``, ``5``, ``6``, ``9``, ``2``, ``11` `]; ` `n ``=` `len``(a); ` `m ``=` `2``; ` ` `  `print``(countSubarrays(a, n, m)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# program to count the number of  ` `// subarrays with m odd numbers ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// function that returns the count of ` `    ``// subarrays with m odd numbers  ` `    ``static` `int` `countSubarrays(``int` `[]a, ` `                            ``int` `n, ``int` `m) ` `    ``{ ` `         `  `        ``int` `count = 0; ` ` `  `        ``// traverse for all possible ` `        ``// subarrays ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` `            ``int` `odd = 0; ` `            ``for` `(``int` `j = i; j < n; j++)  ` `            ``{ ` `                ``if` `(a[j] % 2 ==0) ` `                    ``odd++; ` `     `  `                ``// if count of odd numbers ` `                ``// in subarray is m ` `                ``if` `(odd == m) ` `                    ``count++; ` `            ``} ` `        ``} ` `         `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `[]a = { 2, 2, 5, 6, 9, 2, 11 }; ` `        ``int` `n = a.Length; ` `        ``int` `m = 2; ` `         `  `        ``Console.WriteLine( ` `                  ``countSubarrays(a, n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 ` `

Output :

```8
```

Time Complexity: O(n2)

Efficient Approach: An efficient approach is to while traversing, compute the prefix[] array. Prefix[i] stores the number of prefixes which has ‘i’ odd numbers in it. We increase the count of odd numbers if the array element is an odd one. When the count of odd numbers exceeds or is equal to m, add the number of prefixes which has “(odd-m)” numbers to the answer. At every step odd>=m, we calculate the number of subarrays formed till a particular index with the help of prefix array. prefix[odd-m] provides us with the number of prefixes which has “odd-m” odd numbers, which is added to the count to get the number of subarrays till the index.

Below is the implementation of the above approach:

## C++

 `// CPP program to count the Number  ` `// of subarrays with m odd numbers ` `// O(N) approach ` `#include ` `using` `namespace` `std; ` ` `  `// function that returns the count ` `// of subarrays with m odd numbers ` `int` `countSubarrays(``int` `a[], ``int` `n, ``int` `m) ` `{ ` `    ``int` `count = 0; ` `    ``int` `prefix[n] = { 0 }; ` `    ``int` `odd = 0; ` ` `  `    ``// traverse in the array ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` ` `  `        ``prefix[odd]++; ` ` `  `        ``// if array element is odd ` `        ``if` `(a[i] & 1) ` `            ``odd++; ` ` `  `        ``// when number of odd elements>=M ` `        ``if` `(odd >= m) ` `            ``count += prefix[odd - m]; ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = { 2, 2, 5, 6, 9, 2, 11 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``int` `m = 2; ` `     `  `    ``cout << countSubarrays(a, n, m); ` `     `  `    ``return` `0; ` `} `

## Java

 `// Java program to count the  ` `// number of subarrays with ` `// m odd numbers ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// function that returns the count of ` `    ``// subarrays with m odd numbers  ` `    ``public` `static` `int` `countSubarrays(``int` `a[], ` `                                     ``int` `n, ``int` `m) ` `    ``{ ` `        ``int` `count = ``0``; ` `        ``int` `prefix[] = ``new` `int``[n]; ` `        ``int` `odd = ``0``; ` `     `  `        ``// traverse in the array ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``prefix[odd]++; ` `     `  `            ``// if array element is odd ` `            ``if` `((a[i] & ``1``) == ``1``) ` `                ``odd++; ` `     `  `            ``// when number of odd  ` `            ``// elements >= M ` `            ``if` `(odd >= m) ` `                ``count += prefix[odd - m]; ` `        ``} ` `     `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `a[] = { ``2``, ``2``, ``5``, ``6``, ``9``, ``2``, ``11` `}; ` `        ``int` `n = a.length; ` `        ``int` `m = ``2``; ` `         `  `        ``System.out.println(countSubarrays(a, n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by akash1295. `

## Python3

 `# Python3 program to count the Number  ` `# of subarrays with m odd numbers  ` `# O(N) approach  ` ` `  `# function that returns the count  ` `# of subarrays with m odd numbers  ` `def` `countSubarrays(a, n, m):  ` `    ``count ``=` `0` `    ``prefix ``=` `[``0``] ``*` `n  ` `    ``odd ``=` `0` ` `  `    ``# traverse in the array  ` `    ``for` `i ``in` `range``(n): ` `        ``prefix[odd] ``+``=` `1` ` `  `        ``# if array element is odd  ` `        ``if` `(a[i] & ``1``):  ` `            ``odd ``+``=` `1` ` `  `        ``# when number of odd elements>=M  ` `        ``if` `(odd >``=` `m):  ` `            ``count ``+``=` `prefix[odd ``-` `m]  ` ` `  `    ``return` `count  ` `     `  `# Driver Code  ` `a ``=` `[``2``, ``2``, ``5``, ``6``, ``9``, ``2``, ``11``] ` `n ``=` `len``(a)  ` `m ``=` `2` ` `  `print``(countSubarrays(a, n, m)) ` ` `  `# This code is contributed 29Ajaykumar `

## C#

 `// C# program to count the number of ` `// subarrays with m odd numbers ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// function that returns the count of ` `    ``// subarrays with m odd numbers  ` `    ``public` `static` `int` `countSubarrays(``int` `[]a, ` `                                ``int` `n, ``int` `m) ` `    ``{ ` `        ``int` `count = 0; ` `        ``int` `[]prefix = ``new` `int``[n]; ` `        ``int` `odd = 0; ` `     `  `        ``// traverse in the array ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``prefix[odd]++; ` `     `  `            ``// if array element is odd ` `            ``if` `((a[i] & 1) == 1) ` `                ``odd++; ` `     `  `            ``// when number of odd  ` `            ``// elements >= M ` `            ``if` `(odd >= m) ` `                ``count += prefix[odd - m]; ` `        ``} ` `     `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `[]a = { 2, 2, 5, 6, 9, 2, 11 }; ` `        ``int` `n = a.Length; ` `        ``int` `m = 2; ` `         `  `        ``Console.WriteLine( ` `                  ``countSubarrays(a, n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## PHP

 `=M  ` `        ``if` `(``\$odd` `>= ``\$m``)  ` `            ``\$count` `+= ``\$prefix``[``\$odd` `- ``\$m``];  ` `    ``}  ` ` `  `    ``return` `\$count``;  ` `}  ` ` `  `// Driver Code  ` `\$a` `= ``array``(2, 2, 5, 6, 9, 2, 11 );  ` `\$n` `= sizeof(``\$a``);  ` `\$m` `= 2;  ` ` `  `echo` `countSubarrays(``\$a``, ``\$n``, ``\$m``);  ` ` `  `// This code is contributed  ` `// by Shivi_Aggarwal ` `?> `

Output :

```8
```

Time Complexity: O(n)

My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

8

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.