Given a sequence of n numbers. The task is to count all the subsets of the given set which only have even numbers and all are distinct.

**Note:** By the property of sets, if two subsets have the same set of elements then they are considered as one. For example: [2, 4, 8] and [4, 2, 8] are considered to be the same.

Examples:

Input : {4, 2, 1, 9, 2, 6, 5, 3} Output : 7 The subsets are:[4],[2],[6],[4, 2],[2, 6],[4, 6],[4, 2, 6]Input : {10, 3, 4, 2, 4, 20, 10, 6, 8, 14, 2, 6, 9} Output : 127

A **simple approach** is to consider all the subsets and check whether they satisfy the given conditions or not. The time complexity will be in exponential.

An **efficient approach** is to count number of distinct even numbers. Let this be **ceven**. And then apply formula:

**2 ^{ceven} – 1**

This is similar to counting the number of subsets of a given set of n elements.

**1**is subtracted because the null set is not considered.

[sourcecode language=”CPP”]

// C++ implementation to count subsets having

// even numbers only and all are distinct

#include <bits/stdc++.h>

using namespace std;

// function to count the

// required subsets

int countSubsets(int arr[], int n)

{

unordered_set<int> us;

int even_count = 0;

// inserting even numbers in the set ‘us’

// single copy of each number is retained

for (int i=0; i<n; i++)

if (arr[i] % 2 == 0)

us.insert(arr[i]);

unordered_set<int>:: iterator itr;

// counting distinct even numbers

for (itr=us.begin(); itr!=us.end(); itr++)

even_count++;

// total count of required subsets

return (pow(2, even_count) – 1);

}

// Driver program to test above

int main()

{

int arr[] = {4, 2, 1, 9, 2, 6, 5, 3};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Number of subsets = "

<< countSubsets(arr, n);

return 0;

}

[/sourcecode]

Output:

Number of subsets = 7

Time Complexity: O(n)

This article is contributed by **Ayush Jauhari**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count minimum number of subsets (or subsequences) with consecutive numbers
- Number of distinct subsets of a set
- Minimum number of subsets with distinct elements
- Count no. of ordered subsets having a particular XOR value
- Count number of subsets having a particular XOR value
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Sum of all subsets of a set formed by first n natural numbers
- Sum of sum of all subsets of a set formed by first N natural numbers
- Count distinct elements in an array
- Count distinct Bitwise OR of all Subarrays
- Count all distinct pairs with difference equal to k
- Count number of Distinct Substring in a String
- Absolute distinct count in a sorted array
- Count of subsequences having maximum distinct elements
- Count distinct elements in every window of size k