-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum.cpp
More file actions
154 lines (130 loc) · 4.15 KB
/
combinationSum.cpp
File metadata and controls
154 lines (130 loc) · 4.15 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// { Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution {
public:
//Function to return a list of indexes denoting the required
//combinations whose sum is equal to given number.
/* void findNumbers(vector<int>& ar, int sum,
vector<vector<int> >& res, vector<int>& r,
int i)
{
// if we get exact answer
if (sum == 0) {
res.push_back(r);
return;
}
// Recur for all remaining elements that
// have value smaller than sum.
while (i < ar.size() && sum - ar[i] >= 0) {
// Till every element in the array starting
// from i which can contribute to the sum
r.push_back(ar[i]); // add them to list
// recur for next numbers
findNumbers(ar, sum - ar[i], res, r, i);
i++;
// Remove number from list (backtracking)
r.pop_back();
}
}
// Returns all combinations
// of ar[] that have given
// sum.
vector<vector<int> > combinationSum(vector<int>& ar,
int sum)
{
// sort input array
sort(ar.begin(), ar.end());
// remove duplicates
ar.erase(unique(ar.begin(), ar.end()), ar.end());
vector<int> r;
vector<vector<int> > res;
findNumbers(ar, sum, res, r, 0);
return res;
}*/
/*void solve(vector<int> &a, set<vector<int>> &ans,
int b, int ind, vector<int> &selected,
int selectedSum) {
// cout << ind << " " << selectedSum << endl;
if (selectedSum == b) {
ans.insert(selected);
return;
}
if (selectedSum > b || ind == a.size() || a[ind] > b) return;
solve(a, ans, b, ind + 1, selected, selectedSum);
selected.push_back(a[ind]);
selectedSum += a[ind];
solve(a, ans, b, ind, selected, selectedSum);
solve(a, ans, b, ind + 1, selected, selectedSum);
selectedSum -= a[ind];
selected.pop_back();
}
vector<vector<int>> combinationSum(vector<int> &A, int B) {
set<vector<int>> ans;
vector<vector<int>> res;
vector<int> temp;
sort(A.begin(), A.end());
A.erase(unique(A.begin(), A.end()), A.end());
solve(A, ans, B, 0, temp, 0);
for (auto ele : ans) res.push_back(ele);
return res;
}*/
void findNumbers(vector<int>& a, int sum,
vector<vector<int>>& res, vector<int>& r,
int ind) {
if (sum == 0) {
res.push_back(r);
return;
}
while (ind < a.size() && sum - a[ind] >= 0) {
r.push_back(a[ind]);
findNumbers(a, sum - a[ind], res, r, ind);
ind++;
r.pop_back();
}
}
vector<vector<int> > combinationSum(vector<int>& ar,
int sum) {
sort(ar.begin(), ar.end());
ar.erase(unique(ar.begin(), ar.end()), ar.end());
vector<int> r;
vector<vector<int> > res;
findNumbers(ar, sum, res, r, 0);
return res;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> A;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
A.push_back(x);
}
int sum;
cin >> sum;
Solution ob;
vector<vector<int>> result = ob.combinationSum(A, sum);
if (result.size() == 0) {
cout << "Empty";
}
for (int i = 0; i < result.size(); i++) {
cout << '(';
for (int j = 0; j < result[i].size(); j++) {
cout << result[i][j];
if (j < result[i].size() - 1)
cout << " ";
}
cout << ")";
}
cout << "\n";
}
} // } Driver Code Ends