Given an array, find the number of subsequences whose sum is even and the number of subsequences whose sum is odd.
Example:
Input: arr[] = {1, 2, 2, 3}
Output: EvenSum = 7, OddSum = 8
There are
possible subsequences.
The subsequences with even sum is
1) {1, 3} Sum = 4
2) {1, 2, 2, 3} Sum = 8
3) {1, 2, 3} Sum = 6 (Of index 1)
4) {1, 2, 3} Sum = 6 (Of index 2)
5) {2} Sum = 2 (Of index 1)
6) {2, 2} Sum = 4
7) {2} Sum = 2 (Of index 2)
and the rest subsequence is of odd sum.
Input: arr[] = { 2, 2, 2, 2 }
Output: EvenSum = 15, OddSum = 0
Naive Approach:
One simple approach is to generate all possible subsequences recursively and count the number of subsequences with an even sum and then subtract from total subsequences and the number will be of odd subsequence. The time complexity of this approach will be
.
Better Approach:
A better approach will be using Dynamic programming.
- We would be calculating the count of even subsequences as we iterate through the array. we create 2 arrays countODD[N] and countEVEN[N], where countODD[i] denotes the number of subsequences with odd sum in range
and countEVEN[i] denotes the number of subsequences with even sum in range 
- If we are at position i, and the number is ODD then the total number of subsequences with an even sum would be
- For countEVEN[i], the i-th number is not paired with any other subsequence (i.e. even subsequences till
position) + ith number is paired with all other odd subsequences till
position (odd+odd=even) - For countODD[i], the i-th number is not paired with any other subsequence(i.e. odd subsequences till
position) + ith number is paired with all other even subsequences till
position (odd+even=odd) + one subsequence with only 1 element i.e the ith number itself
- If we are at position i, and the number is EVEN then the total number of subsequences with an even sum would be
- For countEVEN[i], the i-th number is not paired with any other subsequence (i.e. even subsequences till
position) + i-th number is paired with all other even subsequences till
position (even+even=even) + one subsequence with only 1 element i.e the i-th number itself - For countODD[i], the i-th number is not paired with any other subsequence (i.e. odd subsequences till
position) + i-th number is paired with all other odd subsequences till
position (even+odd=odd)
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
pair< int , int > countSum( int arr[], int n)
{
int result = 0;
int countODD[n + 1], countEVEN[n + 1];
countODD[0] = 0;
countEVEN[0] = 0;
for ( int i = 1; i <= n; i++) {
if (arr[i - 1] % 2 == 0) {
countEVEN[i] = countEVEN[i - 1]
+ countEVEN[i - 1] + 1;
countODD[i] = countODD[i - 1]
+ countODD[i - 1];
}
else {
countEVEN[i] = countEVEN[i - 1]
+ countODD[i - 1];
countODD[i] = countODD[i - 1]
+ countEVEN[i - 1] + 1;
}
}
return { countEVEN[n], countODD[n] };
}
int main()
{
int arr[] = { 1, 2, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
pair< int , int > ans = countSum(arr, n);
cout << "EvenSum = " << ans.first;
cout << " OddSum = " << ans.second;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
public static int [] countSum( int arr[], int n)
{
int result = 0 ;
int [] countODD = new int [n + 1 ];
int [] countEVEN = new int [n + 1 ];
countODD[ 0 ] = 0 ;
countEVEN[ 0 ] = 0 ;
for ( int i = 1 ; i <= n; i++)
{
if (arr[i - 1 ] % 2 == 0 )
{
countEVEN[i] = countEVEN[i - 1 ] +
countEVEN[i - 1 ] + 1 ;
countODD[i] = countODD[i - 1 ] +
countODD[i - 1 ];
}
else
{
countEVEN[i] = countEVEN[i - 1 ] +
countODD[i - 1 ];
countODD[i] = countODD[i - 1 ] +
countEVEN[i - 1 ] + 1 ;
}
}
int [] ans = new int [ 2 ];
ans[ 0 ] = countEVEN[n];
ans[ 1 ] = countODD[n];
return ans;
}
public static void main (String[] args)
{
int [] arr = new int []{ 1 , 2 , 2 , 3 };
int n = 4 ;
int [] ans = countSum(arr, n);
System.out.println( "EvenSum = " + ans[ 0 ]);
System.out.println( "OddSum = " + ans[ 1 ]);
}
}
|
Python3
def countSum(arr, n):
result = 0
count_odd = 0
count_even = 0
for i in range (n):
if arr[i - 1 ] % 2 = = 0 :
count_even = count_even + count_even + 1
count_odd = count_odd + count_odd
else :
temp = count_even
count_even = count_even + count_odd
count_odd = count_odd + temp + 1
return [count_even, count_odd]
arr = [ 1 , 2 , 2 , 3 ]
n = len (arr)
ans = countSum(arr, n)
print ( 'EvenSum =' , ans[ 0 ],
'OddSum =' , ans[ 1 ])
|
C#
using System;
class GFG
{
public static int [] countSum( int []arr, int n)
{
int [] countODD = new int [n + 1];
int [] countEVEN = new int [n + 1];
countODD[0] = 0;
countEVEN[0] = 0;
for ( int i = 1; i <= n; i++)
{
if (arr[i - 1] % 2 == 0)
{
countEVEN[i] = countEVEN[i - 1] +
countEVEN[i - 1] + 1;
countODD[i] = countODD[i - 1] +
countODD[i - 1];
}
else
{
countEVEN[i] = countEVEN[i - 1] +
countODD[i - 1];
countODD[i] = countODD[i - 1] +
countEVEN[i - 1] + 1;
}
}
int [] ans = new int [2];
ans[0] = countEVEN[n];
ans[1] = countODD[n];
return ans;
}
public static void Main (String[] args)
{
int [] arr = new int []{ 1, 2, 2, 3 };
int n = 4;
int [] ans = countSum(arr, n);
Console.WriteLine( "EvenSum = " + ans[0]);
Console.WriteLine( "OddSum = " + ans[1]);
}
}
|
Javascript
<script>
function countSum(arr, n)
{
var countODD = Array(n+1).fill(0);
var countEVEN = Array(n+1).fill(0);
countODD[0] = 0;
countEVEN[0] = 0;
for ( var i = 1; i <= n; i++)
{
if (arr[i - 1] % 2 == 0)
{
countEVEN[i] = countEVEN[i - 1] +
countEVEN[i - 1] + 1;
countODD[i] = countODD[i - 1] +
countODD[i - 1];
}
else
{
countEVEN[i] = countEVEN[i - 1] +
countODD[i - 1];
countODD[i] = countODD[i - 1] +
countEVEN[i - 1] + 1;
}
}
var ans = [0,0];
ans[0] = countEVEN[n];
ans[1] = countODD[n];
return ans;
}
var arr = [ 1, 2, 2, 3 ];
var n = 4;
var ans = countSum(arr, n);
document.write( "EvenSum = " + ans[0]);
document.write( " OddSum = " + ans[1]);
</script>
|
Output: EvenSum = 7 OddSum = 8
Time Complexity: O(N).
Space Complexity: O(N) where N is the number of elements in the array.
Efficient Approach:
Instead of making countEVEN[N] and countODD[N] arrays we only need the count_even variable and count_odd variable and changing it the same way as we did earlier.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
pair< int , int > countSum( int arr[], int n)
{
int result = 0;
int count_odd, count_even;
count_odd = 0;
count_even = 0;
for ( int i = 1; i <= n; i++) {
if (arr[i - 1] % 2 == 0) {
count_even = count_even + count_even + 1;
count_odd = count_odd + count_odd;
}
else {
int temp = count_even;
count_even = count_even + count_odd;
count_odd = count_odd + temp + 1;
}
}
return { count_even, count_odd };
}
int main()
{
int arr[] = { 1, 2, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
pair< int , int > ans = countSum(arr, n);
cout << "EvenSum = " << ans.first;
cout << " OddSum = " << ans.second;
return 0;
}
|
Java
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static pair countSum( int arr[], int n)
{
int result = 0 ;
int count_odd, count_even;
count_odd = 0 ;
count_even = 0 ;
for ( int i = 1 ; i <= n; i++)
{
if (arr[i - 1 ] % 2 == 0 )
{
count_even = count_even + count_even + 1 ;
count_odd = count_odd + count_odd;
}
else
{
int temp = count_even;
count_even = count_even + count_odd;
count_odd = count_odd + temp + 1 ;
}
}
return new pair(count_even, count_odd );
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 3 };
int n = arr.length;
pair ans = countSum(arr, n);
System.out.print( "EvenSum = " + ans.first);
System.out.print( " OddSum = " + ans.second);
}
}
|
Python3
def countSum(arr, n):
result = 0
count_odd = 0
count_even = 0
for i in range ( 1 , n + 1 ):
if (arr[i - 1 ] % 2 = = 0 ):
count_even = count_even + count_even + 1
count_odd = count_odd + count_odd
else :
temp = count_even
count_even = count_even + count_odd
count_odd = count_odd + temp + 1
return (count_even, count_odd)
arr = [ 1 , 2 , 2 , 3 ];
n = len (arr)
count_even, count_odd = countSum(arr, n);
print ( "EvenSum = " , count_even,
" OddSum = " , count_odd)
|
C#
using System;
class GFG
{
public class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static pair countSum( int []arr, int n)
{
int count_odd, count_even;
count_odd = 0;
count_even = 0;
for ( int i = 1; i <= n; i++)
{
if (arr[i - 1] % 2 == 0)
{
count_even = count_even + count_even + 1;
count_odd = count_odd + count_odd;
}
else
{
int temp = count_even;
count_even = count_even + count_odd;
count_odd = count_odd + temp + 1;
}
}
return new pair(count_even, count_odd );
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 2, 3 };
int n = arr.Length;
pair ans = countSum(arr, n);
Console.Write( "EvenSum = " + ans.first);
Console.Write( " OddSum = " + ans.second);
}
}
|
Javascript
<script>
var first, second;
function pair( first, second)
{
this .first = first;
this .second = second;
}
function countSum(arr, n)
{
var result = 0;
var count_odd, count_even;
count_odd = 0;
count_even = 0;
for ( var i = 1; i <= n; i++)
{
if (arr[i - 1] % 2 == 0)
{
count_even = count_even + count_even + 1;
count_odd = count_odd + count_odd;
}
else
{
var temp = count_even;
count_even = count_even + count_odd;
count_odd = count_odd + temp + 1;
}
}
return new pair(count_even, count_odd );
}
var arr = [ 1, 2, 2, 3 ];
var n = arr.length;
var ans = countSum(arr, n);
document.write( "EvenSum = " + ans.first);
document.write( " OddSum = " + ans.second);
</script>
|
Output: EvenSum = 7 OddSum = 8
Time Complexity: O(N).
Space Complexity: O(1), where N is the number of elements in the array.