class Solution {
public:
vector<int> buffer;
vector< vector<int> > ans;
void print( vector<int> &array, int n, int k, int target, int index ) {
if( target < 0 ) {
return;
}
if( target == 0 ) {
ans.push_back(buffer);
/*for(int i = 0 ; i < index ; i++ ) {
cout << buffer[i] << " ";
}*/
return;
}
for( int i = k ; i < n ; i++ ) {
buffer.push_back(array[i]);
//buffer[index] = array[i];
print(array,n, i,target-array[i],index+1);
buffer.pop_back();
}
}
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(),candidates.end());
print(candidates,candidates.size(),0,target,0);
return ans;
}
};
Arpit's Newsletter read by 15000+ engineers
🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.