-
Notifications
You must be signed in to change notification settings - Fork 1
/
Combination Sum - Part 2 .cpp
45 lines (40 loc) · 1.05 KB
/
Combination Sum - Part 2 .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
#include<bits/stdc++.h>
using namespace std;
bool combinations(int, int, vector<int>&, vector<int>&);
int main(){
int t, n, k;
cin >> t;
while(t--){
cin >> n;
vector<int> arr(n), res;
for(int i = 0; i < n; i++)
cin >> arr[i];
cin >> k;
sort(arr.begin(), arr.end());
if(!combinations(0, k, arr, res))
cout << "Empty";
cout << endl;
}
return 0;
}
bool combinations(int i, int k, vector<int> &arr, vector<int> &res){
if(k == 0){
cout << "(" << res[0];
for(int i = 1; i < res.size(); i++)
cout << " " << res[i];
cout << ")";
return true;
}
else if(i == arr.size() || k < 0)
return false;
else{
bool flag = false;
res.push_back(arr[i]);
flag = combinations(i + 1, k - arr[i], arr, res) || flag;
res.pop_back();
while(i + 1 < arr.size() && arr[i] == arr[i + 1])
i++;
flag = combinations(i + 1, k, arr, res) || flag;
return flag;
}
}