C++ Backtracking (Short and Sweet :) )


#1
vector<vector<int> > globalBox;
 getSubset(vector<int>&v,int idx,vector<int> &A,int n, int B){
    if(B<0)
        return;
    if(B == 0){
        globalBox.push_back(v);
        return;
    }
    for(int i=idx;i<n;i++){
        if(i==0 || (i>0 && A[i-1]!=A[i])){
            v.push_back(A[i]);
            getSubset(v,i,A,n,B-A[i]);
            v.pop_back();
        }
    }
}
vector<vector<int> > Solution::combinationSum(vector<int> &A, int B) {
    globalBox.clear();
    vector<int>v;
    sort(A.begin(),A.end());
    getSubset(v,0,A,A.size(),B);
    v.clear();
    return globalBox;
    
}   

NOTE : (i>0 && A[i-1]!=A[i])
This condition is to remove duplicates as they are already calculated.