Count subsets having distinct even numbers
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:
2ceven – 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubsets( int arr[], int n)
{
unordered_set< int > us;
int even_count = 0;
for ( int i=0; i<n; i++)
if (arr[i] % 2 == 0)
us.insert(arr[i]);
unordered_set< int >:: iterator itr;
even_count = us.size();
return ( pow (2, even_count) - 1);
}
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;
}
|
Java
import java.util.*;
class GFG
{
static int countSubsets( int arr[], int n)
{
HashSet<Integer> us = new HashSet<>();
int even_count = 0 ;
for ( int i = 0 ; i < n; i++)
if (arr[i] % 2 == 0 )
us.add(arr[i]);
even_count=us.size();
return ( int ) (Math.pow( 2 , even_count) - 1 );
}
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));
}
}
|
Python3
def countSubSets(arr, n):
us = set ()
even_count = 0
for i in range (n):
if arr[i] % 2 = = 0 :
us.add(arr[i])
even_count = len (us)
return pow ( 2 , even_count) - 1
arr = [ 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 ]
n = len (arr)
print ( "Numbers of subset=" , countSubSets(arr,n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countSubsets( int []arr, int n)
{
HashSet< int > us = new HashSet< int >();
int even_count = 0;
for ( int i = 0; i < n; i++)
if (arr[i] % 2 == 0)
us.Add(arr[i]);
even_count = us.Count;
return ( int ) (Math.Pow(2, even_count) - 1);
}
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));
}
}
|
Javascript
<script>
function countSubsets(arr, n)
{
let us = new Set();
let even_count = 0;
for (let i = 0; i < n; i++)
if (arr[i] % 2 == 0)
us.add(arr[i]);
even_count = us.size;
return Math.floor(Math.pow(2, even_count) - 1);
}
let arr = [ 4, 2, 1, 9, 2, 6, 5, 3 ];
let n = arr.length;
document.write( "Number of subsets = " +
countSubsets(arr, n));
</script>
|
Output
Number of subsets = 7
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
31 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...