c++ generate all subsets

#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	// this is the length of the array of values
	// change variable "len" accordingly
	int len = 5;
	// this is the array of values
	int values[] = {3, 4, 2, 8, 5};
	
	// all subsets will be in vector "subsets"
	vector<vector<int>> subsets;
	for (int i = 0; i < pow(2, len); i++) {
		int t = i;
		vector<int> v;
		for (int j = 0; j < len; j++) {
			if (t & 1)
				v.push_back(values[j]);
			t >>= 1;
		}
		subsets.push_back(v);
	}

	// print all of the subsets (optional)
	cout << "subsets:\n";
	for (const vector<int>& subset: subsets) {
		for (const int& value: subset)
			cout << value << " ";
		cout << "\n";
	}
	// note: an empty line will be printed at the top,
	// indicating an empty subset
}

4.75
4

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

4.75 (4 Votes)
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 subset in c++ generate all unique subsets of an array Print unique subsets And Variations c++ generate all subsets n^2 print all unique subsets generate all subsets c++ given an array print all unique subsets with a given sum print unique subsets program how do you generate subsets generate all subsets in c++ subset of array cpp 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++ 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 all subsets of size k c++ generate subsets in with unique and duplicate values generate subsets in C++ how to get all subsets of a list in python using bitmasking generate all subsets most effective way c++ produce every subset in optimized creating subset using bitmasking all subsequences of an array using bit masking subsets of array in c++ how to make a subset in c++ subset of array c++ inbuilt function print all subsets of an array c++ find subsets of an integer array with distinct elements print all subset of all length generating all the sub seta of array bit masking find subset in c++ how to find all subset of a set in c++ continous subset of a set cpp print all possible sub=sets of a string in cpp subset of a set gfg print all subsets of a set c++ subset of array in cpp find repeated substt subsets of array cpp generate subset in c++ dynamic programming dind all subsets of a string print subsets in c++ all subsets of an array c++ subsets of a set c++ generate subsets of set c++ generate subsets using bitmasking subset of a number gfg c++ get all subsets print subsets of an array in c++ print unique subsets find all distinct subsets of a given set having duplicate value find all subset of array having duplicate subsets generator c++ subset generation without single elements c++ subset generation 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++ Find all distinct subsets of a given set backtracking select subset of array cpp generate all subsets of a vector c++ subset c++ subset in c++ finding all subsets of an array in c++ find number o subset in an array c++ all subset of a set c++ generate ordered sequence subsets of sets c++ subsets in c++ find all distinct subsets of a given array all possible subsets of an array c++ subsets of set c++ subset of array c++ how to make subsets in c++ print all unique subset of array subset() in c++ al subsets in c++ c++ generate all subsets
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