print all unique subsets

Solutions on MaxInterview for print all unique subsets by the best coders in the world

showing results for - "print all unique subsets"
Liah
22 Feb 2019
1#include <iostream>
2#include <vector>
3#include <algorithm>
4using namespace std;
5 
6// Function to print the elements of a vector
7void printVector(vector<int> const &out)
8{
9    for (int i: out)
10        cout << i << " ";
11    cout << '\n';
12}
13 
14// Recursive function to print all distinct subsets of S
15// S    --> input set
16// out  --> vector to store subset
17// i    --> index of next element in set S to be processed
18void findPowerSet(int S[], vector<int> &out, int i)
19{
20    // if all elements are processed, print the current subset
21    if (i < 0)
22    {
23        printVector(out);
24        return;
25    }
26 
27    // include current element in the current subset and recur
28    out.push_back(S[i]);
29    findPowerSet(S, out, i - 1);
30 
31    // exclude current element in the current subset
32    out.pop_back(); // backtrack
33 
34    // remove adjacent duplicate elements
35    while (S[i] == S[i-1])
36        i--;
37 
38    // exclude current element in the current subset and recur
39    findPowerSet(S, out, i - 1);
40}
41 
42// Program to generate all distinct subsets of given set
43int main()
44{
45    int S[] = { 1, 3, 1 };
46    int n = sizeof(S) / sizeof(S[0]);
47 
48    // sort the set
49    sort(S, S + n);
50 
51    // create an empty vector to store elements of a subset
52    vector<int> out;
53    findPowerSet(S, out, n-1);
54 
55    return 0;
56}
57
queries leading to this page
subset of array in cppgiven an array print all unique subsets with a given sumgenerate all unique subsets of an arrayprint all possible sub 3dsets of a string in cppprint all subsets of an array in c 2b 2bc 2b 2b generate all subsetssubsets of an array c 2b 2ball subsequences of an array using bit maskinggiven a set of n elements print all subsets of the set using bit maskinggenerating all the sub seta of array bit maskingdynamic programming dind all subsets of a stringprint all subset c 2b 2bsubsets in array c 2b 2bsubsets of array cppprogram for finding the subset of a numberfind all subsets of an array c 2b 2bprint all subsets of a set c 2b 2bprint unique subsetshow to generate subsets in c 2b 2bfind subsets of an integer array with distinct elementsgiven an array arr 5b 5d of integers of size n that might contain duplicates 2c the task is to find all possible unique subsetsall subsets of an array c 2b 2bunique subsetsubsets of array in c 2b 2bgenerate subsets in with unique and duplicate valuesfinding all subsets of an array c 2b 2bcreating subset using bitmaskingfind all unique subsets of arrayprint all unique subsets of arraysubset in c 2b 2bc 2b 2b get all subsetsproduce every subset in optimizedfind all distinct subsets of a given set having duplicate valueprint unique subsets and variationsfind all distinct subsets of a given setcontinous subset of a set cppgenerate all unique subsets of a setfind repeated substtgenerate all subset of string cppfind all subset of array having duplicatehow to get all subsets of a list in python using bitmaskingsubset of array c 2b 2bfind subset in c 2b 2bprint all unique subsetssubset of a set gfgsubset c 2b 2bprint unique subsets programsubsets of a set c 2b 2bsubset of array c 2b 2b inbuilt functionsubset generation without single elements c 2b 2bgenerate subsets using bitmaskingprint all unique subsets