Simple C++ Solution using Backtracking


#1
`void util(vector<vector<int>> &res, vector<int> v, vector<int> &A, int T, int sum, int n)

{
if(sum>T || n<0) return;
if(sum==T)
{
sort(v.begin(),v.end());
if(find(res.begin(),res.end(),v)==res.end()) res.push_back(v);
return;
}
if(sum+A[n-1]<=T)
{
sum+=A[n-1];
v.push_back(A[n-1]);
util(res,v,A,T,sum,n-1);
v.pop_back();
sum-=A[n-1];
util(res,v,A,T,sum,n-1);
}
else
{
util(res,v,A,T,sum,n-1);
}
return;
}

vector<vector > Solution::combinationSum(vector &A, int B)
{
vector<vector>res;
vectorv;
util(res,v,A,B,0,A.size());
sort(res.begin(),res.end());
return res;
}
`