Whats problem with the code?

can i define sets;

hi,
u can definetly define a set of vectors like set<vector < int > > st;`

Usually u would be given the constraint doing it without a set
I have code done the solution, do see it, it will help u for solving such problems in future where unique records are needed.

can you please explain the for loop of sum ?

please explain me the logic

see lets take an example
10 1 2 7 6 1 5
target sum 8
sort it first
1 1 2 5 6 7 10
for(int i = pos ; i < A.size() ; i++){
if(i > pos and A[i] == A[i-1]) continue;
temp.push_back(A[i]);
sum(A, i+1, target-A[i] , temp);
temp.pop_back();
}
first is we start from i = 0
we get to the if() condition only if we have once added a value and in the next iteration we encounter a duplicate value, since when a duplicate is encountered , it would lead to creation of duplicated vector<> , we skip that character.
Else the rest of the code is similar to the condition of finding combination sum

Just the main catch is follow the basic iteration when first a digit is encountered, make all possible combination that make up to target value, next if the same character is encoutered do not proccess it , just continue.

also why didnt you called a function when we have not chosen that element as for every element there is a case of not including and including it

Hey ,
The for loop is doing that for me

Bascially i am adding every element then if target becomes -ve that element is popped out
Then the for loop continues for other values
So the case of taking and not taking in involved using the for loop
You might watch some videos where there is the recursion tree shown, or try it by yourself how the recursion works here

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.