Given an array **arr[]** of size **N**. The task is to find the number of subsequences whose sum is even and the number of subsequences whose sum is odd.

**Examples:**

Input:arr[] = {1, 2, 2, 3}

Output:EvenSum = 7, OddSum = 8

There are 2^{N}-1 possible subsequences.

The subsequences with even sum are

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

**Approach:** This article already exists here having time complexity of where **N** is the size of array. Visit here before moving further.

- If we can find the number of odd subsequences then we can easily find the number of even subsequences.
- The odd subsequences can be formed in two ways:
- By taking odd number odd times.
- Taking even number and odd number odd time.

- Below are some variables and their definition:
**N**= Total number of element in the array.**Even**= Total number of evens in the array.**Odd**= Total number of odd in the array.**Tseq**= Total number of subsequences.**Oseq**= Total number of subsequences with only odd number.**Eseq**= Total number of subsequences with even number.**OddSumSeq**= Total number of subsequences with odd sum.**EvenSumSeq**= Total number of subsequences with even sum.

Tseq = 2^{N}– 1 = Even Subsequence + OddSubsequence

Eseq = 2^{Even}

Oseq =+ . . . .^{Odd}C_{1}+^{Odd}C_{3}+^{Odd}C_{5}

where= Choosing^{Odd}C_{1}1Odd

= Choosing^{Odd}C_{3}3Odd and so onWe can reduce the above equation by this identity, Hence

Oseq = 2^{Odd – 1}So, hence for the total odd sum subsequence, it can be calculated by multiplying these above result

OddSumSeq = 2^{Even}* 2^{Odd – 1}

EvenSumSeq = 2^{N}– 1 – OddSumSeq

Below is the implementation of the above approach:

## C++

`// CPP program to find number of ` `// Subsequences with Even and Odd Sum ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find number of ` `// Subsequences with Even and Odd Sum ` `pair<` `int` `, ` `int` `> countSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `NumberOfOdds = 0, NumberOfEvens = 0; ` ` ` ` ` `// Counting number of odds ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(arr[i] & 1) ` ` ` `NumberOfOdds++; ` ` ` ` ` `// Even count ` ` ` `NumberOfEvens = n - NumberOfOdds; ` ` ` ` ` `int` `NumberOfOddSubsequences = (1 << NumberOfEvens) ` ` ` `* (1 << (NumberOfOdds - 1)); ` ` ` ` ` `// Total Subsequences is (2^n - 1) ` ` ` `// For NumberOfEvenSubsequences subtract ` ` ` `// NumberOfOddSubsequences from total ` ` ` `int` `NumberOfEvenSubsequences = (1 << n) - 1 ` ` ` `- NumberOfOddSubsequences; ` ` ` ` ` `return` `{ NumberOfEvenSubsequences, ` ` ` `NumberOfOddSubsequences }; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 2, 3 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Calling the function ` ` ` `pair<` `int` `, ` `int` `> ans = countSum(arr, n); ` ` ` ` ` `cout << ` `"EvenSum = "` `<< ans.first; ` ` ` `cout << ` `" OddSum = "` `<< ans.second; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of ` `// Subsequences with Even and Odd Sum ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `static` `class` `pair ` `{ ` ` ` `int` `first, second; ` ` ` `public` `pair(` `int` `first, ` `int` `second) ` ` ` `{ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` `} ` ` ` `// Function to find number of ` `// Subsequences with Even and Odd Sum ` `static` `pair countSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `NumberOfOdds = ` `0` `, NumberOfEvens = ` `0` `; ` ` ` ` ` `// Counting number of odds ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `if` `(arr[i] % ` `2` `== ` `1` `) ` ` ` `NumberOfOdds++; ` ` ` ` ` `// Even count ` ` ` `NumberOfEvens = n - NumberOfOdds; ` ` ` ` ` `int` `NumberOfOddSubsequences = (` `1` `<< NumberOfEvens) * ` ` ` `(` `1` `<< (NumberOfOdds - ` `1` `)); ` ` ` ` ` `// Total Subsequences is (2^n - 1) ` ` ` `// For NumberOfEvenSubsequences subtract ` ` ` `// NumberOfOddSubsequences from total ` ` ` `int` `NumberOfEvenSubsequences = (` `1` `<< n) - ` `1` `- ` ` ` `NumberOfOddSubsequences; ` ` ` ` ` `return` `new` `pair(NumberOfEvenSubsequences, ` ` ` `NumberOfOddSubsequences); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `2` `, ` `3` `}; ` ` ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Calling the function ` ` ` `pair ans = countSum(arr, n); ` ` ` ` ` `System.out.print(` `"EvenSum = "` `+ ans.first); ` ` ` `System.out.print(` `" OddSum = "` `+ ans.second); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find number of ` `# Subsequences with Even and Odd Sum ` ` ` `# Function to find number of ` `# Subsequences with Even and Odd Sum ` `def` `countSum(arr, n) : ` ` ` ` ` `NumberOfOdds ` `=` `0` `; NumberOfEvens ` `=` `0` `; ` ` ` ` ` `# Counting number of odds ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `if` `(arr[i] & ` `1` `) : ` ` ` `NumberOfOdds ` `+` `=` `1` `; ` ` ` ` ` `# Even count ` ` ` `NumberOfEvens ` `=` `n ` `-` `NumberOfOdds; ` ` ` ` ` `NumberOfOddSubsequences ` `=` `(` `1` `<< NumberOfEvens) ` `*` `\ ` ` ` `(` `1` `<< (NumberOfOdds ` `-` `1` `)); ` ` ` ` ` `# Total Subsequences is (2^n - 1) ` ` ` `# For NumberOfEvenSubsequences subtract ` ` ` `# NumberOfOddSubsequences from total ` ` ` `NumberOfEvenSubsequences ` `=` `(` `1` `<< n) ` `-` `1` `-` `\ ` ` ` `NumberOfOddSubsequences; ` ` ` ` ` `return` `(NumberOfEvenSubsequences, ` ` ` `NumberOfOddSubsequences); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `2` `, ` `3` `]; ` ` ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `# Calling the function ` ` ` `ans ` `=` `countSum(arr, n); ` ` ` ` ` `print` `(` `"EvenSum ="` `, ans[` `0` `], end ` `=` `" "` `); ` ` ` `print` `(` `"OddSum ="` `, ans[` `1` `]); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of ` `// Subsequences with Even and Odd Sum ` `using` `System; ` ` ` `class` `GFG ` `{ ` `public` `class` `pair ` `{ ` ` ` `public` `int` `first, second; ` ` ` `public` `pair(` `int` `first, ` `int` `second) ` ` ` `{ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` `} ` ` ` `// Function to find number of ` `// Subsequences with Even and Odd Sum ` `static` `pair countSum(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `int` `NumberOfOdds = 0, NumberOfEvens = 0; ` ` ` ` ` `// Counting number of odds ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(arr[i] % 2 == 1) ` ` ` `NumberOfOdds++; ` ` ` ` ` `// Even count ` ` ` `NumberOfEvens = n - NumberOfOdds; ` ` ` ` ` `int` `NumberOfOddSubsequences = (1 << NumberOfEvens) * ` ` ` `(1 << (NumberOfOdds - 1)); ` ` ` ` ` `// Total Subsequences is (2^n - 1) ` ` ` `// For NumberOfEvenSubsequences subtract ` ` ` `// NumberOfOddSubsequences from total ` ` ` `int` `NumberOfEvenSubsequences = (1 << n) - 1 - ` ` ` `NumberOfOddSubsequences; ` ` ` ` ` `return` `new` `pair(NumberOfEvenSubsequences, ` ` ` `NumberOfOddSubsequences); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 1, 2, 2, 3 }; ` ` ` ` ` `int` `n = arr.Length; ` ` ` ` ` `// Calling the function ` ` ` `pair ans = countSum(arr, n); ` ` ` ` ` `Console.Write(` `"EvenSum = "` `+ ans.first); ` ` ` `Console.Write(` `" OddSum = "` `+ ans.second); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

EvenSum = 7 OddSum = 8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number of Subsequences with Even and Odd Sum
- Check if a number has an odd count of odd divisors and even count of even divisors
- Even numbers at even index and odd numbers at odd index
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest odd number with even sum of digits from the given number N
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Number of permutations such that sum of elements at odd index and even index are equal
- Find the sum of digits of a number at even and odd places
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Generate an array of given size with equal count and sum of odd and even numbers
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count ways to express even number ānā as sum of even integers
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1

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.