-
Notifications
You must be signed in to change notification settings - Fork 4
/
subsequence.cpp
83 lines (69 loc) · 1.55 KB
/
subsequence.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
const int N = 2e5 + 11;
int n;
int li[10];
void subs(vector<int> v, int k) {
if (k == n) {
for (auto x : v)cout << x << " ";
cout << endl;
} else {
subs(v, k + 1);
v.push_back(li[k]);
subs(v, k + 1);
v.pop_back();
}
}
// Itereative
// for (int b = 0; b < (1<<n); b++) {
// vector<int> subset;
// for (int i = 0; i < n; i++) {
// if (b&(1<<i)) subset.push_back(i);
// }
// }
void solve() {
cin >> n;
for (int i = 0; i < n; i++)cin >> li[i];
vector<int> sub;
subs(sub, 0);
}
int main() {
freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int no_of_test_cases;
// cin >> no_of_test_cases;
no_of_test_cases = 1;
while (no_of_test_cases--) solve();
return 0;
}
// Second way
class Solution {
void traverse(vector<int>& nums,
vector<int>& calc,
vector<vector<int>> &res,
int numberTaken) {
if (numberTaken == size(nums)) {
res.push_back(calc);
return;
}
calc.push_back(nums[numberTaken]);
traverse(nums, calc, res, numberTaken + 1);
calc.pop_back();
traverse(nums, calc, res, numberTaken + 1);
}
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
vector<int> calc;
traverse(nums, calc, res, 0);
return res;
}
};