-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathCombinationSum.cpp
55 lines (47 loc) · 1.1 KB
/
CombinationSum.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
#include<bits/stdc++.h>
using namespace std;
void solve(vector<vector<int>>&ans, vector<int>&v, vector<int>&candidates, int target, int n)
{
if(target==0)
{
ans.push_back(v);
return;
}
if(n<=0 || target<0)
{
return ;
}
v.push_back(candidates[n-1]);
solve(ans, v, candidates, target-candidates[n-1], n);
v.pop_back();
solve(ans, v, candidates, target, n-1);
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>>ans;
vector<int>v;
int n=candidates.size();
solve(ans, v, candidates, target, n);
return ans;
}
int main()
{
int n, a, target;
cout<<"No of elements : ";
cin>>n;
for(int i=0; i<n; i++)
{
cin>>a;
candidates.push_back(a);
}
cout<<"Enter the target : ";
cin>>target;
vector<vector<int>>ans=combinationSum(candidates, target);
cout<<"Required sets are : ";
for(auto it:ans)
{
for(auto x: it)
cout<<x<<" ";
cout<<endl;
}
return 0;
}