Simplest solution to change in subset problem


#1

Comment body goes
void dfs(vector& nums, vector& ans, vector<vector>& res, int start,int target) {

     if(target<0)    //when target is negative then stop
     {
         return;
     }
     else if(target==0)      //when target is equal to zero that's why we got a answer
                             //part and push into vector
     {
          res.push_back(ans);
         return ;
     }
    for (int i = start; i < nums.size(); i++) {
      
        ans.push_back(nums[i]);
        dfs(nums, ans, res, i ,target-nums[i]);   //not modified i bcz we have to choose 
                                                  //one element multiple times
        ans.pop_back();
    }
}

vector<vector > Solution::combinationSum(vector &nums, int target) {
vector ans;
vector<vector> res;
sort(nums.begin(), nums.end());
dfs(nums, ans, res, 0,target);
set<vector>st(res.begin(),res.end());
res.clear();
for(auto it:st)
{
res.push_back(it);
}
return res;
}
s here.