Pure Recusive Code - No iteration


#1

void solve(vector &a,int index,int sum,vector &temp,vector<vector> &ans)
{

if(sum<0)
return;
if(sum==0)    
{
    ans.push_back(temp);
    return;
}
if(index== a.size())
    return ;

    temp.push_back(a[index]);
    solve(a,index,sum-a[index],temp,ans);
    temp.pop_back();
    
solve(a,index+1,sum,temp,ans);

return;

}

vector<vector > Solution::combinationSum(vector &A, int B) {
vector<vector> res;
vector temp;
sort(A.begin(),A.end());
solve(A,0,B,temp,res);
// sort(ans.begin(),ans.end());

set<vector<int>> st(res.begin(),res.end());
res.clear();
for(auto it:st)
{
res.push_back(it);
}
return res;

}