- Related Questions & Answers
- Count number of subsets having a particular XOR value in C++
- Count of subsequences having maximum distinct elements in C++
- Count numbers having 0 as a digit in C++
- Count subarrays having total distinct elements same as original array in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Count distinct pairs from two arrays having same sum of digits in C++
- Find the count of maximum contiguous Even numbers in C++
- Find all distinct subsets of a given set in C++
- Count n digit numbers not having a particular digit in C++
- 8085 program to count total even numbers in series of 10 numbers
- Count subsets that satisfy the given condition in C++
- Count Odd and Even numbers in a range from L to R in C++
- Count of numbers having only 1 set bit in the range [0, n] in C++
- Python program to Count Even and Odd numbers in a List
- Subsets II in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given an array of positive integers. The goal is to find the subsets of numbers in an array such that each subset has distinct even numbers in it. All sets with the same elements will be counted as 1. [2,4,6] and [6,2,4] are the same set.

Let us understand with examples

**Input** − arr[] = {1,3,5,7,8,3,2 };

**Output** −Count of subsets having distinct even numbers are − 3

**Explanation** − Subsets will be − [2], [8], [2,8]

**Input** − arr[] = {2,4,6 };

**Output** −Count of subsets having distinct even numbers are − 7

**Explanation** − Subsets will be − [2], [4], [6], [2,4], [2,6], [4,6], [2,4,6]

We make a set of all even numbers in the array. This gives a count of distinct even numbers. The formula will be 2^{even count} - 1

Take an array of numbers arr[].

Functionsubset_even(int arr[], int size) takes an array of numbers and returns the subsets with distinct even numbers.

Take the initial count as 0.

Create an unordered_set<int> un_set for even numbers.

Traverse arr[] using for loop. From i=0 to i<length.

If arr[i]%2==0, it is even. Insert to un_set.

Take count=un_set.size() // distinct even numbers.

Update count=pow(2,count) - 1.

Return count as result.

#include <bits/stdc++.h> using namespace std; int subset_even(int arr[], int size){ int count = 0; unordered_set<int> un_set; for(int i=0; i<size; i++){ if (arr[i] % 2 == 0){ un_set.insert(arr[i]); } } unordered_set<int>:: iterator i; count = un_set.size(); count = pow(2, count) - 1; return count; } int main(){ int arr[] = {10, 4, 21, 3, 5, 7, 6, 8}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of subsets having distinct even numbers are: "<<subset_even(arr, size); return 0; }

If we run the above code it will generate the following output −

Count of subsets having distinct even numbers are: 15

Advertisements