Skip to content

Commit f867806

Browse files
authored
Merge pull request #636 from ayan2809/master
Added Recursive solution to subset problem
2 parents 01d28db + 09d90d6 commit f867806

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

Recursion/subsets.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
The problem is to generate all the possible subsets of the given input vector
3+
4+
Given an integer array nums of unique elements, return all possible subsets (the power set).
5+
The solution set must not contain duplicate subsets. Return the solution in any order.
6+
7+
Example 1:
8+
9+
Input: nums = [1,2,3]
10+
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
11+
Example 2:
12+
13+
Input: nums = [0]
14+
Output: [[],[0]]
15+
16+
The problem is given below
17+
https://leetcode.com/problems/subsets/
18+
19+
Below is the recursive appraoch of the problem-
20+
*/
21+
22+
#include<bits/stdc++.h>
23+
using namespace std;
24+
25+
vector<vector<int>> op; //global vector to store the output
26+
27+
void solve(vector<int> input, vector<int> & output)
28+
{
29+
if(input.size()==0) //when the size of the input vector is 0, we have to add the output to the global vector
30+
{
31+
vector<int> sum;
32+
for(auto it: output)
33+
sum.push_back(it); //we have to convert the vector to a vector of integers
34+
op.push_back(sum); //push the vector to the global vector
35+
return; //exit the function
36+
}
37+
vector<int> out1=output; //first output vector will store our choice one of the recursive tree
38+
vector<int> out2=output; //second output vector will store our choice two of the recursive tree
39+
//we do not push anything in the first vector
40+
out2.push_back(input[0]); //push the first element to the second output vector
41+
input.erase(input.begin()+0); //erase the first element of the input vector
42+
solve(input,out1); //recursive call
43+
solve(input,out2); //recursive call
44+
}
45+
vector<vector<int>> subsets(vector<int>& nums) {
46+
vector<int> output;
47+
solve(nums, output);
48+
vector<vector<int>> out=op;
49+
return out;
50+
}
51+
int main()
52+
{
53+
int t=0;
54+
cin>>t;
55+
while(t--) {
56+
int n=0; //input of the vector length
57+
cin>>n;
58+
vector<int>nums(n);
59+
for(int i=0;i<n;i++) {
60+
cin>>nums[i];
61+
}
62+
vector<vector<int>> out=subsets(nums);
63+
64+
//printing the output vector
65+
for(auto it:out) {
66+
for(auto it1:it) {
67+
cout<<it1<<" ";
68+
}
69+
cout<<endl;
70+
}
71+
cout<<endl;
72+
}
73+
}

Recursion/subsets2.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
The problem is to generate all the possible subsets of the given input vector and should not print any duplicate elements.
3+
4+
5+
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
6+
7+
The solution set must not contain duplicate subsets. Return the solution in any order.
8+
9+
Example 1:
10+
11+
Input: nums = [1,2,2]
12+
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
13+
Example 2:
14+
15+
Input: nums = [0]
16+
Output: [[],[0]]
17+
18+
The problem is given below
19+
https://leetcode.com/problems/subsets-ii/
20+
21+
Below is the recursive appraoch of the problem-
22+
*/
23+
24+
25+
#include<bits/stdc++.h> //input of the header files
26+
using namespace std;
27+
set<vector<int>> s; //declaing a set to store only the unique elements
28+
void solve(vector<int> input, vector<int> & output) //recursive function to generate the unique subsets
29+
{
30+
if(input.size()==0)
31+
{
32+
vector<int> sum; //declaring a vector to store the combinations of the elements
33+
sort(output.begin(),output.end()); //sorting it to make prevent duplicates entering the set
34+
for(auto it: output)
35+
sum.push_back(it);
36+
s.insert(sum); //inserting the unique elements in the set
37+
return; //return to end the function
38+
}
39+
vector<int> out1=output; //declaring a vector to store the elements of the subset
40+
vector<int> out2=output; //declaring another vector to store the elements of the subset
41+
42+
out2.push_back(input[0]); //inserting the first element in the second vector
43+
input.erase(input.begin()+0); //erase function to delete the first element of the vector
44+
solve(input,out1); //recursive call to generate the subsets with the first vector
45+
solve(input,out2); //recursive call to generate the subsets with the second vector
46+
}
47+
vector<vector<int>> subsetsWithDup(vector<int>& nums) { //function which call the solve function to generate the subsets
48+
vector<int> output;
49+
solve(nums, output);
50+
vector<vector<int>> out;
51+
for(auto it:s)
52+
{
53+
out.push_back(it);
54+
}
55+
56+
return out; //returning the subsets after duplicates are removed
57+
}
58+
int main()
59+
{
60+
int t=0;
61+
cin>>t;
62+
while(t--) {
63+
int n=0; //input of the vector length
64+
cin>>n;
65+
vector<int>nums(n);
66+
for(int i=0;i<n;i++) {
67+
cin>>nums[i];
68+
}
69+
vector<vector<int>> out=subsetsWithDup(nums);
70+
71+
//printing the output vector
72+
for(auto it:out) {
73+
for(auto it1:it) {
74+
cout<<it1<<" ";
75+
}
76+
cout<<endl;
77+
}
78+
cout<<endl;
79+
}
80+
}

0 commit comments

Comments
 (0)