print all unique subsets

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
// Function to print the elements of a vector
void printVector(vector<int> const &out)
{
    for (int i: out)
        cout << i << " ";
    cout << '\n';
}
 
// Recursive function to print all distinct subsets of S
// S    --> input set
// out  --> vector to store subset
// i    --> index of next element in set S to be processed
void findPowerSet(int S[], vector<int> &out, int i)
{
    // if all elements are processed, print the current subset
    if (i < 0)
    {
        printVector(out);
        return;
    }
 
    // include current element in the current subset and recur
    out.push_back(S[i]);
    findPowerSet(S, out, i - 1);
 
    // exclude current element in the current subset
    out.pop_back(); // backtrack
 
    // remove adjacent duplicate elements
    while (S[i] == S[i-1])
        i--;
 
    // exclude current element in the current subset and recur
    findPowerSet(S, out, i - 1);
}
 
// Program to generate all distinct subsets of given set
int main()
{
    int S[] = { 1, 3, 1 };
    int n = sizeof(S) / sizeof(S[0]);
 
    // sort the set
    sort(S, S + n);
 
    // create an empty vector to store elements of a subset
    vector<int> out;
    findPowerSet(S, out, n-1);
 
    return 0;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
find all unique subsets of array generate all unique subsets of an array Print unique subsets And Variations print all unique subsets given an array print all unique subsets with a given sum print unique subsets program generate all subset of string cpp generate all unique subsets of a set subsets of an array c++ finding all subsets of an array c++ subsets of array in c++ how to generate subsets in c++ given a set of n elements print all subsets of the set using bit masking Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets generate subsets in with unique and duplicate values how to get all subsets of a list in python using bitmasking produce every subset in optimized creating subset using bitmasking all subsequences of an array using bit masking subset of array c++ inbuilt function find subsets of an integer array with distinct elements generating all the sub seta of array bit masking find subset in c++ continous subset of a set cpp print all possible sub=sets of a string in cpp subset of a set gfg subset c++ print all subsets of a set c++ subset of array in cpp find repeated substt subset in c++ subsets of array cpp dynamic programming dind all subsets of a string subset of array c++ c++ generate all subsets all subsets of an array c++ subsets of a set c++ generate subsets using bitmasking c++ get all subsets print unique subsets find all distinct subsets of a given set having duplicate value find all subset of array having duplicate subset generation without single elements c++ unique subset print all subset c++ subsets in array c++ program for finding the subset of a number find all subsets of an array c++ Find all distinct subsets of a given set print all subsets of an array in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source