# Number of sub arrays with odd sum

Given an array, find the number of subarrays whose sum is odd.

Examples:

```Input : arr[] = {5, 4, 4, 5, 1, 3}
Output : 12

There are possible subarrays with odd
sum. The subarrays are
1) {5} Sum = 5 (At index 0)
2) {5, 4}  Sum = 9
3) {5, 4, 4}  Sum = 13
4) {5, 4, 4, 5, 1} Sum = 19
5) {4, 4, 5}  Sum = 13
6) {4, 4, 5, 1, 3}  Sum = 17
7) {4, 5}  Sum = 9
8) {4, 5, 1, 3} Sum = 13
9) {5}  Sum = 5 (At index 3)
10) {5, 1, 3}  Sum = 9
11)  {1} Sum = 1
12) {3} Sum = 3
```

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

O(n2) time and O(1) space method [Brute Force]
We can simply generate all the possible sub-arrays and find whether the sum of all the elements in them is an odd or not. If it is odd then we will count that sub-array otherwise neglect it.

## C++

 `// C++ code to find count of sub-arrays ` `// with odd sum ` `#include ` `using` `namespace` `std; ` ` `  `int` `countOddSum(``int` `ar[], ``int` `n) ` `{ ` `    ``int` `result = 0; ` ` `  `    ``// Find sum of all subarrays and increment ` `    ``// result if sum is odd ` `    ``for` `(``int` `i = 0; i <= n - 1; i++) { ` `        ``int` `val = 0; ` `        ``for` `(``int` `j = i; j <= n - 1; j++) { ` `            ``val = val + ar[j]; ` `            ``if` `(val % 2 != 0) ` `                ``result++; ` `        ``} ` `    ``} ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `ar[] = { 5, 4, 4, 5, 1, 3 }; ` `    ``int` `n = ``sizeof``(ar) / ``sizeof``(ar); ` ` `  `    ``cout << ``"The Number of Subarrays with odd"` `            ``" sum is "` `         ``<< countOddSum(ar, n); ` ` `  `    ``return` `(0); ` `} `

## Java

 `// Java code to find count of sub-arrays ` `// with odd sum ` `import` `java.io.*; ` ` `  `class` `GFG { ` `    ``static` `int` `countOddSum(``int` `ar[], ` `                           ``int` `n) ` `    ``{ ` `        ``int` `result = ``0``; ` ` `  `        ``// Find sum of all subarrays ` `        ``// and increment result if ` `        ``// sum is odd ` `        ``for` `(``int` `i = ``0``; i <= n - ``1``; i++) { ` `            ``int` `val = ``0``; ` `            ``for` `(``int` `j = i; j <= n - ``1``; j++) { ` `                ``val = val + ar[j]; ` `                ``if` `(val % ``2` `!= ``0``) ` `                    ``result++; ` `            ``} ` `        ``} ` ` `  `        ``return` `(result); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `ar[] = { ``5``, ``4``, ``4``, ``5``, ``1``, ``3` `}; ` `        ``int` `n = ar.length; ` ` `  `        ``System.out.print(``"The Number of Subarrays"` `                         ``+ ``" with odd sum is "``); ` ` `  `        ``System.out.println(countOddSum(ar, n)); ` `    ``} ` `} `

## Python 3

 `# Python 3 code to find count  ` `# of sub-arrays with odd sum ` ` `  `def` `countOddSum(ar, n): ` `    ``result ``=` `0` ` `  `    ``# Find sum of all subarrays and  ` `    ``# increment result if sum is odd ` `    ``for` `i ``in` `range``(n): ` `        ``val ``=` `0` `        ``for` `j ``in` `range``(i, n ):  ` `            ``val ``=` `val ``+` `ar[j] ` `            ``if` `(val ``%` `2` `!``=` `0``): ` `                ``result ``+``=``1` ` `  `    ``return` `(result) ` ` `  `# Driver code ` `ar ``=` `[ ``5``, ``4``, ``4``, ``5``, ``1``, ``3` `] ` ` `  `print``(``"The Number of Subarrays"` `,  ` `            ``"with odd"``, end ``=` `"") ` ` `  `print``(``" sum is "``+` `str``(countOddSum(ar, ``6``))) ` ` `  `# This code is contributed ` `# by ChitraNayal `

## C#

 `// C# code to find count  ` `// of sub-arrays with odd sum ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `static` `int` `countOddSum(``int` `[]ar, ` `                       ``int` `n) ` `{ ` `    ``int` `result = 0; ` ` `  `    ``// Find sum of all subarrays ` `    ``// and increment result if ` `    ``// sum is odd ` `    ``for` `(``int` `i = 0; ` `             ``i <= n - 1; i++)  ` `    ``{ ` `        ``int` `val = 0; ` `        ``for` `(``int` `j = i;  ` `                 ``j <= n - 1; j++)  ` `        ``{ ` `            ``val = val + ar[j]; ` `            ``if` `(val % 2 != 0) ` `                ``result++; ` `        ``} ` `    ``} ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]ar = {5, 4, 4, 5, 1, 3}; ` `    ``int` `n = ar.Length; ` ` `  `    ``Console.Write(``"The Number of Subarrays"` `+  ` `                        ``" with odd sum is "``); ` ` `  `    ``Console.WriteLine(countOddSum(ar, n)); ` `} ` `} ` ` `  `// This code is contributed ` `// by chandan_jnu. `

## PHP

 ` `

Output :

`The Number of Subarrays with odd sum is 12`

O(n) Time and O(1) Space Method [Efficient]
If we do compute the cumulative sum array in temp[] of our input array, then we can see that the sub-array starting from i and ending at j, has an even sum if temp[] if (temp[j] – temp[i]) % 2 = 0. So, instead of building a cumulative sum array we build a cumulative sum modulo 2 array. Then calculating odd-even pairs will give the required result i.e. temp*temp.

## C++

 `// C++ proggram to find count of sub-arrays ` `// with odd sum ` `#include ` `using` `namespace` `std; ` ` `  `int` `countOddSum(``int` `ar[], ``int` `n) ` `{ ` `    ``// A temporary array of size 2. temp is ` `    ``// going to store count of even subarrays ` `    ``// and temp count of odd. ` `    ``// temp is initialized as 1 because there ` `    ``// a single odd element is also counted as ` `    ``// a subarray ` `    ``int` `temp = { 1, 0 }; ` ` `  `    ``// Initialize count. sum is sum of elements ` `    ``// under modulo 2 and ending with arr[i]. ` `    ``int` `result = 0, val = 0; ` ` `  `    ``// i'th iteration computes sum of arr[0..i] ` `    ``// under modulo 2 and increments even/odd count ` `    ``// according to sum's value ` `    ``for` `(``int` `i = 0; i <= n - 1; i++) { ` `        ``// 2 is added to handle negative numbers ` `        ``val = ((val + ar[i]) % 2 + 2) % 2; ` ` `  `        ``// Increment even/odd count ` `        ``temp[val]++; ` `    ``} ` ` `  `    ``// An odd can be formed by even-odd pair ` `    ``result = (temp * temp); ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `ar[] = { 5, 4, 4, 5, 1, 3 }; ` `    ``int` `n = ``sizeof``(ar) / ``sizeof``(ar); ` ` `  `    ``cout << ``"The Number of Subarrays with odd"` `            ``" sum is "` `         ``<< countOddSum(ar, n); ` ` `  `    ``return` `(0); ` `} `

## Java

 `// Java code to find count of sub-arrays ` `// with odd sum ` `import` `java.io.*; ` ` `  `class` `GFG { ` `    ``static` `int` `countOddSum(``int` `ar[], ` `                           ``int` `n) ` `    ``{ ` `        ``// A temporary array of size 2. ` `        ``// temp is going to store ` `        ``// count of even subarrays ` `        ``// and temp count of odd. ` `        ``// temp is initialized as ` `        ``// 1 because there a single odd ` `        ``// element is also counted as ` `        ``// a subarray ` `        ``int` `temp[] = { ``1``, ``0` `}; ` ` `  `        ``// Initialize count. sum is ` `        ``// sum of elements under modulo ` `        ``// 2 and ending with arr[i]. ` `        ``int` `result = ``0``, val = ``0``; ` ` `  `        ``// i'th iteration computes sum ` `        ``// of arr[0..i] under modulo 2 ` `        ``// and increments even/odd count ` `        ``// according to sum's value ` `        ``for` `(``int` `i = ``0``; i <= n - ``1``; i++) { ` `            ``// 2 is added to handle ` `            ``// negative numbers ` `            ``val = ((val + ar[i]) % ``2` `+ ``2``) % ``2``; ` ` `  `            ``// Increment even/odd count ` `            ``temp[val]++; ` `        ``} ` ` `  `        ``// An odd can be formed by an even-odd pair ` `        ``result = temp[``0``] * temp[``1``]; ` ` `  `        ``return` `(result); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``int` `ar[] = { ``5``, ``4``, ``4``, ``5``, ``1``, ``3` `}; ` `        ``int` `n = ar.length; ` ` `  `        ``System.out.println(``"The Number of Subarrays"` `                           ``+ ``" with odd sum is "` `+ countOddSum(ar, n)); ` `    ``} ` `} `

## Python 3

 `# Python 3 proggram to   ` `# find count of sub-arrays ` `# with odd sum ` `def` `countOddSum(ar, n): ` `     `  `    ``# A temporary array of size  ` `    ``# 2. temp is going to  ` `    ``# store count of even subarrays ` `    ``# and temp count of odd. ` `    ``# temp is initialized as 1  ` `    ``# because there is a single odd  ` `    ``# element is also counted as ` `    ``# a subarray ` `    ``temp ``=` `[ ``1``, ``0` `] ` ` `  `    ``# Initialize count. sum is sum  ` `    ``# of elements under modulo 2  ` `    ``# and ending with arr[i]. ` `    ``result ``=` `0` `    ``val ``=` `0` ` `  `    ``# i'th iteration computes  ` `    ``# sum of arr[0..i] under  ` `    ``# modulo 2 and increments  ` `    ``# even/odd count according ` `    ``# to sum's value ` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# 2 is added to handle ` `        ``# negative numbers ` `        ``val ``=` `((val ``+` `ar[i]) ``%` `2` `+` `2``) ``%` `2` ` `  `        ``# Increment even/odd count ` `        ``temp[val] ``+``=` `1` ` `  `    ``# An odd can be formed  ` `    ``# by even-odd pair ` `    ``result ``=` `(temp[``0``] ``*` `temp[``1``]) ` ` `  `    ``return` `(result) ` ` `  `# Driver code ` `ar ``=` `[ ``5``, ``4``, ``4``, ``5``, ``1``, ``3` `] ` ` `  `print``(``"The Number of Subarrays"` `           ``" with odd sum is "``+` `       ``str``(countOddSum(ar, ``6``))) ` `        `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// C# code to find count of  ` `// sub-arrays with odd sum ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `static` `int` `countOddSum(``int``[] ar, ` `                       ``int` `n) ` `{ ` `    ``// A temporary array of size 2. ` `    ``// temp is going to store ` `    ``// count of even subarrays ` `    ``// and temp count of odd. ` `    ``// temp is initialized as ` `    ``// 1 because there a single odd ` `    ``// element is also counted as ` `    ``// a subarray ` `    ``int``[] temp = { 1, 0 }; ` ` `  `    ``// Initialize count. sum is ` `    ``// sum of elements under modulo ` `    ``// 2 and ending with arr[i]. ` `    ``int` `result = 0, val = 0; ` ` `  `    ``// i'th iteration computes sum ` `    ``// of arr[0..i] under modulo 2 ` `    ``// and increments even/odd count ` `    ``// according to sum's value ` `    ``for` `(``int` `i = 0; i <= n - 1; i++) ` `    ``{ ` `        ``// 2 is added to handle ` `        ``// negative numbers ` `        ``val = ((val + ar[i]) % 2 + 2) % 2; ` ` `  `        ``// Increment even/odd count ` `        ``temp[val]++; ` `    ``} ` ` `  `    ``// An odd can be formed  ` `    ``// by an even-odd pair ` `    ``result = temp * temp; ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` `  `    ``int``[] ar = { 5, 4, 4, 5, 1, 3 }; ` `    ``int` `n = ar.Length; ` ` `  `    ``Console.Write(``"The Number of Subarrays"` `+  ` `                        ``" with odd sum is "` `+  ` `                         ``countOddSum(ar, n)); ` `} ` `} ` ` `  `// This code is contributed ` `// by ChitraNayal `

## PHP

 ` `

Output :

`The Number of Subarrays with odd sum is 12`

Another efficient approach is to first find the number of subarrays starting at index 0 and having an odd sum. Then traverse the array and update the number of subarrays starting at index i and having an odd sum.

Below is the implementation of the above approach :

 `// CPP program to find number of subarrays with odd sum ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find number of subarrays with odd sum ` `int` `countOddSum(``int` `a[], ``int` `n)  ` `{ ` `    ``// 'odd' stores number of odd numbers upto ith index ` `    ``// 'c_odd' stores number of odd sum subarrays starting at ith index ` `    ``// 'Result' stores the number of odd sum subarrays ` `    ``int` `odd = 0, c_odd=0, result = 0; ` `     `  `    ``// First find number of odd sum subarrays starting at 0th index  ` `    ``for``(``int` `i=0;i

Output :

`The Number of Subarrays with odd sum is 12`

Time Complexity: O(N)
Space Complexity: O(1)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : Chandan_Kumar, Ita_c, amannidhi

Article Tags :
Practice Tags :

Be the First to upvote.

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