具有重复元素的集合中唯一子集的计数
给定一个大小为N的数组arr[] 。任务是计算唯一子集的数量。
例子:
Input: arr[] = {1, 2, 2}
Output: 6
Explanation: Total possible subsets of this set = 2³= 8.
Following are the all 8 subsets formed form 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
朴素方法:在基本方法中,创建集合的所有子集并将它们存储在仅存储唯一元素的 std::set 中。但这不是一种省时的方法。
时间复杂度: O(2 N )
辅助空间: O(N * 2 N-1 )
有效方法:可以观察到不需要找到所有子集。唯一关心的是找到唯一子集的计数。根据每个元素在唯一子集中贡献的次数,可以通过数学运算来完成,最后得到一个公式。
按照下面的观察得出公式。
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.
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find number of unique subsets
void countNumberofUniqueSubsets(int A[],
int N)
{
// Creating a map to store
// frequency of elements
map m;
// Filling map
for (int i = 0; i < N; i++) {
// Counting frequency of each elements
m[A[i]]++;
}
// Finding product of (frequency+1)
// for each elements
int subsets = 1;
for (auto& value : m)
subsets *= (value.second + 1);
cout << subsets;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countNumberofUniqueSubsets(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find number of unique subsets
static void countNumberofUniqueSubsets(int A[],
int N)
{
// Creating a map to store
// frequency of elements
HashMap m = new HashMap<>();
// Filling map
for (int i = 0; i < N; i++) {
// Counting frequency of each elements
if (m.containsKey(A[i]))
{
m.put(A[i], m.get(A[i]) + 1);
}
else
{
m.put(A[i], 1);
}
}
// Finding product of (frequency+1)
// for each elements
int subsets = 1;
for (Map.Entry value : m.entrySet())
subsets *= (value.getValue() + 1);
System.out.print(subsets);
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 1, 2, 2 };
int N = arr.length;
// Function Call
countNumberofUniqueSubsets(arr, N);
}
}
// This code is contributed by hrithikgarg03188.
Python
# Python code for the above approach
# Function to find number of unique subsets
def countNumberofUniqueSubsets(A, N):
# Creating a map to store
# frequency of elements
m = dict()
# Filling map
for i in range(N):
# Counting frequency of each elements
if (A[i] in m):
m[A[i]] += 1
else:
m[A[i]] = 1
# Finding product of (frequency+1)
# for each elements
subsets = 1
for value in m.values():
subsets = subsets * (value + 1)
print(subsets)
# Driver Code
arr = [1, 2, 2]
N = len(arr)
# Function Call
countNumberofUniqueSubsets(arr, N)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find number of unique subsets
static void countNumberofUniqueSubsets(int []A, int N)
{
// Creating a map to store
// frequency of elements
Dictionary m =
new Dictionary();
// Filling map
for (int i = 0; i < N; i++)
{
// Counting frequency of each elements
if (m.ContainsKey(A[i]))
{
m[A[i]] = m[A[i]] + 1;
}
else
{
m.Add(A[i], 1);
}
}
// Finding product of (frequency+1)
// for each elements
int subsets = 1;
foreach(KeyValuePair value in m)
{
subsets *= (value.Value + 1);
}
Console.Write(subsets);
}
// Driver Code
public static void Main () {
int []arr = { 1, 2, 2 };
int N = arr.Length;
// Function Call
countNumberofUniqueSubsets(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
6
时间复杂度: O(N)
辅助空间: 在)