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.

## C++

`// 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; ` ` ` ` ` `// distinct even numbers ` ` ` `even_count = us.size(); ` ` ` ` ` `// 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count subsets having ` `// even numbers only and all are distinct ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// function to count the ` `// required subsets ` `static` `int` `countSubsets(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `HashSet<Integer> us = ` `new` `HashSet<>(); ` ` ` `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.add(arr[i]); ` ` ` ` ` ` ` `// counting distinct even numbers ` ` ` `even_count=us.size(); ` ` ` ` ` `// total count of required subsets ` ` ` `return` `(` `int` `) (Math.pow(` `2` `, even_count) - ` `1` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = {` `4` `, ` `2` `, ` `1` `, ` `9` `, ` `2` `, ` `6` `, ` `5` `, ` `3` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(` `"Number of subsets = "` ` ` `+ countSubsets(arr, n)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# python implementation to count subsets having ` `# even numbers only and all are distinct ` ` ` `#function to count the required subsets ` `def` `countSubSets(arr, n): ` ` ` `us ` `=` `set` `() ` ` ` `even_count ` `=` `0` ` ` ` ` `# inserting even numbers in the set 'us' ` ` ` `# single copy of each number is retained ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `arr[i] ` `%` `2` `=` `=` `0` `: ` ` ` `us.add(arr[i]) ` ` ` ` ` `# counting distinct even numbers ` ` ` `even_count ` `=` `len` `(us) ` ` ` ` ` `# total count of required subsets ` ` ` `return` `pow` `(` `2` `, even_count)` `-` `1` ` ` ` ` `# Driver program ` `arr ` `=` `[` `4` `, ` `2` `, ` `1` `, ` `9` `, ` `2` `, ` `6` `, ` `5` `, ` `3` `] ` `n ` `=` `len` `(arr) ` `print` `(` `"Numbers of subset="` `, countSubSets(arr,n)) ` ` ` `# This code is contributed by Shrikant13 ` ` ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count subsets having ` `// even numbers only and all are distinct ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// function to count the ` `// required subsets ` `static` `int` `countSubsets(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `HashSet<` `int` `> us = ` `new` `HashSet<` `int` `>(); ` ` ` `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.Add(arr[i]); ` ` ` ` ` ` ` `// counting distinct even numbers ` ` ` `even_count = us.Count; ` ` ` ` ` `// total count of required subsets ` ` ` `return` `(` `int` `) (Math.Pow(2, even_count) - 1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[] arr = {4, 2, 1, 9, 2, 6, 5, 3}; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(` `"Number of subsets = "` ` ` `+ countSubsets(arr, n)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**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.

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 number of subsets having a particular XOR value
- Count no. of ordered subsets having a particular XOR value
- Count of subsets of integers from 1 to N having no adjacent elements
- Count of subsets having sum of min and max element less than K
- Maximize count of subsets having product of smallest element and size of the subset at least X
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Numbers having Unique (or Distinct) digits
- Count subarrays having total distinct elements same as original array
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Minimum number of subsets with distinct elements
- Number of distinct pair of edges such that it partitions both trees into same subsets of nodes
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count non-adjacent subsets from numbers arranged in Circular fashion
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Total number of subsets in which the product of the elements is even
- Longest subarray not having more than K distinct elements