In the generate function when we recursively call the function the value of i is incremented so how are we able to generate the entire power set of the given set.

Acc to me after entire iteration this is the result

[ [] , [1] , [1,2] , [1,2,3] ]

I know I’m wrong and we are backtracking it by popping the last element but how are we manipulating the value of i.

In short can you please explain the recursive call with the incrementation of i.

Problem statement - Given a set of distinct integers, S, return all possible subsets.

void generate(int idx,vector&A,vector&curr,vector<vector>&ans){

ans.push_back(curr);

for(int i=idx;i<A.size();i++){

curr.push_back(A[i]);

generate(i+1,A,curr,ans);

curr.pop_back();

}

return;

}

vector<vector > Solution::subsets(vector &A) {

sort(A.begin(),A.end());

vector<vector>ans;

vectorcurr;

generate(0,A,curr,ans);

return ans;

}