Count of unique subsets from a set having repeated elements
Given an array arr[] of size N. The task is to count the number of unique subsets.
Examples:
Input: arr[] = {1, 2, 2}
Output: 6
Explanation: Total possible subsets of this set = 2³= 8.
Following are the all 8 subsets formed from arr[].
{}, {1}, {2}, {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2}.
These are all possible subsets out of which {2} and {1, 2} are repeated.
Therefore there are only 6 unique subsets of given set.
Input: arr[] = {1, 3, 3, 4, 4, 4}
Output: 24
Naive Approach: In the basic approach, create all the subsets of the set and keep storing them in a std::set that stores only unique elements. But this isn’t a time-efficient approach.
Time Complexity: O(2N)
Auxiliary Space: O(N * 2N-1)
Efficient Approach: It can be observed that it is not required to find all the subsets. The only concern is to find the count of unique subsets. On the basis of how many times each element is contributing in unique subsets, it can be done by mathematical manipulations and finally ending up with a formula.
Follow the observation below to arrive at the formula.
For each unique value vali say the frequency of that element is freq[vali].
So each unique value has (freq[vali] + 1) to be present in a unique subset
because it can be present 0 times, 1 time, 2 times . . . freq[vali] times in a subset.
Now this is true for all such unique elements.
Therefore, The number of unique subsets of a set = Product of (frequency+1) of each element.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void countNumberofUniqueSubsets( int A[],
int N)
{
map< int , int > m;
for ( int i = 0; i < N; i++) {
m[A[i]]++;
}
int subsets = 1;
for ( auto & value : m)
subsets *= (value.second + 1);
cout << subsets;
}
int main()
{
int arr[] = { 1, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
countNumberofUniqueSubsets(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void countNumberofUniqueSubsets( int A[],
int N)
{
HashMap<Integer, Integer> m = new HashMap<>();
for ( int i = 0 ; i < N; i++) {
if (m.containsKey(A[i]))
{
m.put(A[i], m.get(A[i]) + 1 );
}
else
{
m.put(A[i], 1 );
}
}
int subsets = 1 ;
for (Map.Entry<Integer, Integer> value : m.entrySet())
subsets *= (value.getValue() + 1 );
System.out.print(subsets);
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 2 };
int N = arr.length;
countNumberofUniqueSubsets(arr, N);
}
}
|
Python
def countNumberofUniqueSubsets(A, N):
m = dict ()
for i in range (N):
if (A[i] in m):
m[A[i]] + = 1
else :
m[A[i]] = 1
subsets = 1
for value in m.values():
subsets = subsets * (value + 1 )
print (subsets)
arr = [ 1 , 2 , 2 ]
N = len (arr)
countNumberofUniqueSubsets(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void countNumberofUniqueSubsets( int []A, int N)
{
Dictionary< int , int > m =
new Dictionary< int , int >();
for ( int i = 0; i < N; i++)
{
if (m.ContainsKey(A[i]))
{
m[A[i]] = m[A[i]] + 1;
}
else
{
m.Add(A[i], 1);
}
}
int subsets = 1;
foreach (KeyValuePair< int , int > value in m)
{
subsets *= (value.Value + 1);
}
Console.Write(subsets);
}
public static void Main () {
int []arr = { 1, 2, 2 };
int N = arr.Length;
countNumberofUniqueSubsets(arr, N);
}
}
|
Javascript
<script>
function countNumberofUniqueSubsets(A, N)
{
let m = new Map();
for (let i = 0; i < N; i++)
{
if (m.has(A[i])) {
m.set(A[i], m.get(A[i])+ 1)
}
else {
m.set(A[i], 1)
}
}
let subsets = 1;
for (let value of m.values())
subsets = subsets * (value + 1);
document.write(subsets);
}
let arr = [1, 2, 2];
let N = arr.length;
countNumberofUniqueSubsets(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
27 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...