Some subsets are missing..why please help


#1

void helper(vectora,int b,int pos,vector&temp,set<vector>&ans,int sum,int n){
if(sum>b||pos>=n){
return;
}
if(sum==b){
ans.insert(temp);
return;
}
temp.push_back(a[pos]);
helper(a,b,pos+1,temp,ans,sum+a[pos],n);
temp.pop_back();
helper(a,b,pos+1,temp,ans,sum,n);
}
vector<vector > Solution::combinationSum(vector &A, int B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int n=A.size();
vectortemp;
set<vector>ans;
sort(A.begin(),A.end());
helper(A,B,0,temp,ans,0,n);
vector<vector>p;
for(auto it=ans.begin();it!=ans.end();it++){
p.push_back(*it);
}
sort(p.begin(),p.end());
return p;
}


#2

Hello anubhav your second condition that is
if(sum==b){
ans.insert(temp);
return;
} should be the first condition in the code


#3

Hey, can you please tell what difference does it really make? I had the exact same problem in my code and it worked after implementing what you said, but I’m unable to figure out why.
Thanks!


#4

well that’s because when pos==n then also your sum can be equal target which needs to be pushed_back


#5

Thanks bro, same problem