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
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.
Implementation:
C++
#include<iostream>
using namespace std;
int countEvenSum( int arr[], int n)
{
int result = 0;
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);
}
int main()
{
int arr[] = {1, 2, 2, 3, 4, 1};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "The Number of Subarrays with even"
" sum is " << countEvenSum (arr, n);
return (0);
}
|
Java
import java.io.*;
class GFG
{
static int countEvenSum( int arr[],
int n)
{
int result = 0 ;
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);
}
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));
}
}
|
Python3
def countEvenSum(arr, n):
result = 0
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)
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))
|
C#
using System;
class GFG
{
static int countEvenSum( int []arr,
int n)
{
int result = 0;
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);
}
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));
}
}
|
PHP
<?php
function countEvenSum( $arr , $n )
{
$result = 0;
for ( $i = 0; $i <= $n - 1; $i ++)
{
$sum = 0;
for ( $j = $i ; $j <= $n - 1; $j ++)
{
$sum = $sum + $arr [ $j ];
if ( $sum % 2 == 0)
$result ++;
}
}
return ( $result );
}
$arr = array (1, 2, 2, 3, 4, 1);
$n = sizeof ( $arr );
echo "The Number of Subarrays " ,
"with even sum is " ,
countEvenSum ( $arr , $n );
?>
|
Javascript
<script>
function countEvenSum(arr,
n)
{
let result = 0;
for (let i = 0; i <= n - 1; i++)
{
let sum = 0;
for (let j = i; j <= n - 1; j++)
{
sum = sum + arr[j];
if (sum % 2 == 0)
result++;
}
}
return (result);
}
let arr = [1, 2, 2,
3, 4, 1];
let n = arr.length;
document.write( "The Number of Subarrays" +
" with even sum is " );
document.write(countEvenSum(arr, n));
</script>
|
Output
The Number of Subarrays with even sum is 9
Time Complexity: O(n2)
Auxiliary Space: O(1)
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]
Implementation:
C++
#include<iostream>
using namespace std;
int countEvenSum( int arr[], int n)
{
int temp[2] = {1, 0};
int result = 0, sum = 0;
for ( int i=0; i<=n-1; i++)
{
sum = ( (sum + arr[i]) % 2 + 2) % 2;
temp[sum]++;
}
result = result + (temp[0]*(temp[0]-1)/2);
result = result + (temp[1]*(temp[1]-1)/2);
return (result);
}
int main()
{
int arr[] = {1, 2, 2, 3, 4, 1};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "The Number of Subarrays with even"
" sum is " << countEvenSum (arr, n);
return (0);
}
|
Java
import java.io.*;
class GFG
{
static int countEvenSum( int arr[],
int n)
{
int temp[] = { 1 , 0 };
int result = 0 , sum = 0 ;
for ( int i = 0 ; i <= n - 1 ; i++)
{
sum = ((sum + arr[i]) %
2 + 2 ) % 2 ;
temp[sum]++;
}
result = result + (temp[ 0 ] *
(temp[ 0 ] - 1 ) / 2 );
result = result + (temp[ 1 ] *
(temp[ 1 ] - 1 ) / 2 );
return (result);
}
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));
}
}
|
Python 3
def countEvenSum(arr, n):
temp = [ 1 , 0 ]
result = 0
sum = 0
for i in range ( n):
sum = ( ( sum + arr[i]) % 2 + 2 ) % 2
temp[ sum ] + = 1
result = result + (temp[ 0 ] * (temp[ 0 ] - 1 ) / / 2 )
result = result + (temp[ 1 ] * (temp[ 1 ] - 1 ) / / 2 )
return (result)
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))
|
C#
using System;
class GFG
{
static int countEvenSum( int []arr,
int n)
{
int []temp = {1, 0};
int result = 0, sum = 0;
for ( int i = 0; i <= n - 1; i++)
{
sum = ((sum + arr[i]) %
2 + 2) % 2;
temp[sum]++;
}
result = result + (temp[0] *
(temp[0] - 1) / 2);
result = result + (temp[1] *
(temp[1] - 1) / 2);
return (result);
}
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));
}
}
|
PHP
<?php
function countEvenSum( $arr , $n )
{
$temp = array (1, 0);
$result = 0; $sum = 0;
for ( $i = 0; $i <= $n - 1; $i ++)
{
$sum = (( $sum + $arr [ $i ]) %
2 + 2) % 2;
$temp [ $sum ]++;
}
$result = $result + (int)( $temp [0] *
( $temp [0] - 1) / 2);
$result = $result + (int)( $temp [1] *
( $temp [1] - 1) / 2);
return ( $result );
}
$arr = array (1, 2, 2,
3, 4, 1);
$n = sizeof ( $arr );
echo "The Number of Subarrays " .
"with even" , " sum is " ,
countEvenSum ( $arr , $n );
?>
|
Javascript
<script>
function countEvenSum(arr,n)
{
let temp = [1, 0];
let result = 0, sum = 0;
for (let i = 0; i <= n - 1; i++)
{
sum = ((sum + arr[i]) %
2 + 2) % 2;
temp[sum]++;
}
result = result + (temp[0] *
(temp[0] - 1) / 2);
result = result + (temp[1] *
(temp[1] - 1) / 2);
return (result);
}
let arr=[1, 2, 2, 3, 4, 1];
let n = arr.length;
document.write( "The Number of Subarrays" +
" with even sum is " +
countEvenSum (arr, n));
</script>
|
Output
The Number of Subarrays with even sum is 9
Time Complexity: O(n)
Auxiliary Space: O(1)
O(n) Time and O(1) Space Method (bottom-up-approach)
If we start counting from last index and keep track of number of subarrays with even sum so far starting from present index then we can calculate number of subarrays with even sum starting from previous index
Implementation:
C++
#include <iostream>
using namespace std;
long long countEvenSum( int a[], int n)
{
long long res = 0;
int s = 0;
for ( int i = n - 1; i >= 0; i--)
{
if (a[i] % 2 == 1)
{
s = n - i - 1 - s;
}
else
{
s = s + 1;
}
res = res + s;
}
return res;
}
int main()
{
int arr[] = { 1, 2, 2, 3, 4, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "The Number of Subarrays with even"
" sum is "
<< countEvenSum(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static long countEvenSum( int a[], int n)
{
long res = 0 ;
int s = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (a[i] % 2 == 1 )
{
s = n - i - 1 - s;
}
else
{
s = s + 1 ;
}
res = res + s;
}
return res;
}
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));
}
}
|
Python3
def countEvenSum(arr, n):
res = 0
s = 0
for i in reversed ( range (n)):
if arr[i] % 2 = = 1 :
s = n - i - 1 - s
else :
s = s + 1
res = res + s
return res
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))
|
C#
using System;
public class GFG
{
public static long countEvenSum( int [] a, int n)
{
long res = 0;
int s = 0;
for ( int i = n - 1; i >= 0; i--)
{
if (a[i] % 2 == 1)
{
s = n - i - 1 - s;
}
else
{
s = s + 1;
}
res = res + s;
}
return res;
}
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));
}
}
|
Javascript
<script>
function countEvenSum(a, n)
{
let res = 0;
let s = 0;
for (let i = n - 1; i >= 0; i--)
{
if (a[i] % 2 == 1)
{
s = n - i - 1 - s;
}
else
{
s = s + 1;
}
res = res + s;
}
return res;
}
let arr = [ 1, 2, 2, 3, 4, 1 ];
let n = arr.length;
document.write( "The Number of Subarrays" +
" with even sum is " + countEvenSum(arr, n));
</script>
|
Output
The Number of Subarrays with even sum is 9
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...