-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39.cpp
More file actions
70 lines (51 loc) · 1.59 KB
/
Copy path39.cpp
File metadata and controls
70 lines (51 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
// Subset generation using recursion and backtracking
// https://leetcode.com/problems/subsets/description/
vector<vector<int>> subsets;
void generate(vector<int> &subset, int i, vector<int> &nums){ // we don't want to pass by value other wise complexity will be very high
if(i == nums.size()){ // base condition
subsets.push_back(subset);
return;
}
// ith element not in subset
generate(subset, i+1, nums);
// ith element in subset
subset.push_back(nums[i]);
generate(subset, i+1, nums);
subset.pop_back(); // this is called backtracking
}
/*
[]
[1] [] ->1
[1,2] [1] [2] [] ->2
[1, 2, 3] [1,2] [1,3] [1] [2,3] [2] [3] [] ->3
thus there are n levels and thus total functions call = 2^n+1 ~ 2^n
and time complexity of each function is O(1)
// Thus total time complexity is O(2^n)
*/
int main() {
// we know for n elements there are 2^n subsets
/*
input
3
1 2 3
*/
int n;
cin>>n;
vector<int> nums(n);
for (int i = 0; i < n; i++)
{
cin>>nums[i];
}
vector<int> empty;
generate(empty, 0, nums);
for(auto &sub : subsets){
for (auto &elem : sub)
{
cout<<elem<<" ";
}
cout<<endl;
}
return 0;
}