-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum.cpp
More file actions
49 lines (49 loc) · 1.52 KB
/
combinationSum.cpp
File metadata and controls
49 lines (49 loc) · 1.52 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
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_set>
void helper(std::vector<int> &arr, int currIndex, int currSum, int totalSum,std::vector<int> output,std::vector<std::vector<int> >& result, std::unordered_set<std::string> &buffer){
if(currSum==totalSum){
std::string s(output.begin(), output.end());
if(buffer.find(s)==buffer.end()){
//not present
buffer.insert(s);
result.push_back(output);
}
return;
}
else if(currIndex>=arr.size() || currSum>totalSum){
return;
}
//do not take this number
helper(arr, currIndex+1, currSum, totalSum, output,result, buffer);
//take this number
output.push_back(arr[currIndex]);
helper(arr,currIndex+1,currSum+arr[currIndex],totalSum,output,result, buffer);
}
std::vector<std::vector<int> > solve(std::vector<int> &arr, int K){
std::vector<std::vector<int> > returnValue;
std::vector<int> num;
std::sort(arr.begin(),arr.end());
std::unordered_set<std::string> buffer;
helper(arr, 0,0, K,num,returnValue, buffer);
std::reverse(returnValue.begin(),returnValue.end());
return returnValue;
}
int main(){
int N,K;
std::cin>>N;
std::vector<int> arr(N);
for(int index = 0;index<N;++index){
std::cin>>arr[index];
}
std::cin>>K;
std::vector<std::vector<int> > output = solve(arr,K);
for(auto x: output){
for(auto y:x){
std::cout<<y<<" ";
}
std::cout<<std::endl;
}
return 0;
}