# Find number of subarrays with even sum

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

Example :

```Input : arr[] = {1, 2, 2, 3, 4, 1}
Output : 9

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

## Recommended: Please solve it on “PRACTICE” 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 even or not. If it is even then we will count that sub-array otherwise neglect it.

## C++

 `/* C++ program to count number of sub-arrays ` `  ``whose sum is even using brute force ` ` ``Time Complexity - O(N^2) ` ` ``Space Complexity - O(1) */` `#include ` `using` `namespace` `std; ` ` `  `int` `countEvenSum(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `result = 0; ` ` `  `    ``// Find sum of all subarrays and increment ` `    ``// result if sum is even ` `    ``for` `(``int` `i=0; i<=n-1; i++) ` `    ``{ ` `        ``int` `sum = 0; ` `        ``for` `(``int` `j=i; j<=n-1; j++) ` `        ``{ ` `            ``sum = sum + arr[j]; ` `            ``if` `(sum % 2 == 0) ` `                    ``result++; ` `        ``} ` `    ``} ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = {1, 2, 2, 3, 4, 1}; ` `    ``int` `n = ``sizeof` `(arr) / ``sizeof` `(arr); ` ` `  `    ``cout << ``"The Number of Subarrays with even"` `            ``" sum is "` `<< countEvenSum (arr, n); ` ` `  `    ``return` `(0); ` `} `

## Java

 `// Java program to count number  ` `// of sub-arrays whose sum is  ` `// even using brute force  ` `// Time Complexity - O(N^2) ` `// Space Complexity - O(1)  ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `static` `int` `countEvenSum(``int` `arr[],  ` `                        ``int` `n) ` `{ ` `    ``int` `result = ``0``; ` ` `  `    ``// Find sum of all subarrays  ` `    ``// and increment result if ` `    ``// sum is even ` `    ``for` `(``int` `i = ``0``; i <= n - ``1``; i++) ` `    ``{ ` `        ``int` `sum = ``0``; ` `        ``for` `(``int` `j = i; j <= n - ``1``; j++) ` `        ``{ ` `            ``sum = sum + arr[j]; ` `            ``if` `(sum % ``2` `== ``0``) ` `                    ``result++; ` `        ``} ` `    ``} ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `int` `arr[] = {``1``, ``2``, ``2``,  ` `             ``3``, ``4``, ``1``}; ` `int` `n = arr.length; ` ` `  `System.out.print(``"The Number of Subarrays"``+  ` `                     ``" with even sum is "``); ` `                      `  `System.out.println(countEvenSum(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## Python3

 `# Python 3 program to count number  ` `# of sub-arrays whose sum is even ` `# using brute force ` `# Time Complexity - O(N^2) ` `# Space Complexity - O(1)  ` ` `  `def` `countEvenSum(arr, n): ` `    ``result ``=` `0` ` `  `    ``# Find sum of all subarrays and ` `    ``# increment result if sum is even ` `    ``for` `i ``in` `range``(``0``, n, ``1``): ` `        ``sum` `=` `0` `        ``for` `j ``in` `range``(i, n, ``1``): ` `            ``sum` `=` `sum` `+` `arr[j] ` `            ``if` `(``sum` `%` `2` `=``=` `0``): ` `                    ``result ``=` `result ``+` `1` ` `  `    ``return` `(result) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``1``, ``2``, ``2``, ``3``, ``4``, ``1``] ` `    ``n ``=` `len``(arr) ` `    ``print``(``"The Number of Subarrays"` `,  ` `                  ``"with even sum is"``,  ` `               ``countEvenSum (arr, n)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to count number  ` `// of sub-arrays whose sum is  ` `// even using brute force  ` `// Time Complexity - O(N^2) ` `// Space Complexity - O(1) ` `using` `System; ` ` `  `class` `GFG ` `{ ` `static` `int` `countEvenSum(``int` `[]arr,  ` `                        ``int` `n) ` `{ ` `    ``int` `result = 0; ` ` `  `    ``// Find sum of all subarrays  ` `    ``// and increment result if ` `    ``// sum is even ` `    ``for` `(``int` `i = 0; i <= n - 1; i++) ` `    ``{ ` `        ``int` `sum = 0; ` `        ``for` `(``int` `j = i; j <= n - 1; j++) ` `        ``{ ` `            ``sum = sum + arr[j]; ` `            ``if` `(sum % 2 == 0) ` `                    ``result++; ` `        ``} ` `    ``} ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `[]arr = {1, 2, 2,  ` `                 ``3, 4, 1}; ` `    ``int` `n = arr.Length; ` ` `  `    ``Console.Write(``"The Number of Subarrays"``+  ` `                      ``" with even sum is "``); ` `                     `  `    ``Console.WriteLine(countEvenSum(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by m_kit `

## PHP

 ` `

Output :

`The Number of Subarrays with even sum is 9`

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, and find how many times 0 and 1 appears in temp[] array using handshake formula. [n * (n-1) /2]

## C++

 `/* C++ program to count number of sub-arrays ` `with even sum using an efficient algorithm ` `Time Complexity - O(N) ` `Space Complexity - O(1)*/` `#include ` `using` `namespace` `std; ` ` `  `int` `countEvenSum(``int` `arr[], ``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 even 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, sum = 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 ` `        ``sum = ( (sum + arr[i]) % 2 + 2) % 2; ` ` `  `        ``// Increment even/odd count ` `        ``temp[sum]++; ` `    ``} ` ` `  `    ``// Use handshake lemma to count even subarrays ` `    ``// (Note that an even cam be formed by two even ` `    ``// or two odd) ` `    ``result = result + (temp*(temp-1)/2); ` `    ``result = result + (temp*(temp-1)/2); ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = {1, 2, 2, 3, 4, 1}; ` `    ``int` `n = ``sizeof` `(arr) / ``sizeof` `(arr); ` ` `  `    ``cout << ``"The Number of Subarrays with even"` `            ``" sum is "` `<< countEvenSum (arr, n); ` ` `  `    ``return` `(0); ` `} `

## Java

 `// Java program to count  ` `// number of sub-arrays ` `// with even sum using an  ` `// efficient algorithm ` `// Time Complexity - O(N) ` `// Space Complexity - O(1) ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `static` `int` `countEvenSum(``int` `arr[],   ` `                        ``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 even  ` `    ``// 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``, sum = ``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 ` `        ``sum = ((sum + arr[i]) %  ` `                    ``2` `+ ``2``) % ``2``; ` ` `  `        ``// Increment even/odd count ` `        ``temp[sum]++; ` `    ``} ` ` `  `    ``// Use handshake lemma to ` `    ``// count even subarrays ` `    ``// (Note that an even cam  ` `    ``// be formed by two even ` `    ``// or two odd) ` `    ``result = result + (temp[``0``] *  ` `                      ``(temp[``0``] - ``1``) / ``2``); ` `    ``result = result + (temp[``1``] *  ` `                      ``(temp[``1``] - ``1``) / ``2``); ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` ` `  `int` `arr[] = {``1``, ``2``, ``2``, ``3``, ``4``, ``1``}; ` `int` `n = arr.length; ` ` `  `System.out.println(``"The Number of Subarrays"``+  ` `                       ``" with even sum is "` `+ ` `                      ``countEvenSum (arr, n)); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## Python 3

 `# Python 3 program to count number of sub-arrays ` `# with even sum using an efficient algorithm ` `# Time Complexity - O(N) ` `# Space Complexity - O(1) ` `def` `countEvenSum(arr, 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 even 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` `    ``sum` `=` `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 ` `        ``sum` `=` `( (``sum` `+` `arr[i]) ``%` `2` `+` `2``) ``%` `2` ` `  `        ``# Increment even/odd count ` `        ``temp[``sum``]``+``=` `1` ` `  `    ``# Use handshake lemma to count even subarrays ` `    ``# (Note that an even cam be formed by two even ` `    ``# or two odd) ` `    ``result ``=` `result ``+` `(temp[``0``] ``*` `(temp[``0``] ``-` `1``) ``/``/` `2``) ` `    ``result ``=` `result ``+` `(temp[``1``] ``*` `(temp[``1``] ``-` `1``) ``/``/` `2``) ` ` `  `    ``return` `(result) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``arr ``=` `[``1``, ``2``, ``2``, ``3``, ``4``, ``1``] ` `    ``n ``=` `len``(arr) ` `    ``print``( ``"The Number of Subarrays with even"` `            ``" sum is"` `, countEvenSum (arr, n)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to count  ` `// number of sub-arrays ` `// with even sum using an  ` `// efficient algorithm ` `// Time Complexity - O(N) ` `// Space Complexity - O(1) ` `using` `System; ` ` `  `class` `GFG ` `{ ` `static` `int` `countEvenSum(``int` `[]arr,  ` `                        ``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 even  ` `    ``// 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, sum = 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 ` `        ``sum = ((sum + arr[i]) %  ` `                    ``2 + 2) % 2; ` ` `  `        ``// Increment even ` `        ``// or odd count ` `        ``temp[sum]++; ` `    ``} ` ` `  `    ``// Use handshake lemma to ` `    ``// count even subarrays ` `    ``// (Note that an even cam  ` `    ``// be formed by two even ` `    ``// or two odd) ` `    ``result = result + (temp *  ` `                      ``(temp - 1) / 2); ` `    ``result = result + (temp *  ` `                      ``(temp - 1) / 2); ` ` `  `    ``return` `(result); ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `[]arr = {1, 2, 2, 3, 4, 1}; ` `    ``int` `n = arr.Length; ` ` `  `    ``Console.WriteLine(``"The Number of Subarrays"``+  ` `                          ``" with even sum is "` `+ ` `                         ``countEvenSum (arr, n)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by akt_mit `

## PHP

 ` `

Output :

`The Number of Subarrays with even sum is 9`

This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :

5

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