Easily understandable C++ solution with comments


#1

void help(set<vector >&a, vector&t, vector&A, int sum,int pos )
{
if(sum==0)
{ if(a.find(t)==a.end())
a.insert(t);
return;
}
if(pos>=A.size() || sum<0) // return if pos exceeds elements or sum is <0
return;
t.push_back(A[pos]);
help(a,t,A,sum-A[pos],pos); // if including the current point, reduce sum and recursively call.
t.pop_back();
help(a,t,A,sum,pos+1);// if not including, move to next element.
}
vector<vector > Solution::combinationSum(vector &A, int B) {
sort(A.begin(),A.end());
set<vector>ans;// set to check repeated elements
vector<vector>v;// final answer
vectort;
int pos=0;
help(ans,t,A,B,pos);//helper functiom
for(auto it:ans)
v.push_back(it);
return v;
}