How to generate subset

code- https://ide.codingblocks.com/s/327987

Hey @anubhavb11 ,
Can you please elaborate what exactly are you not able to find?
Not able to traverse to find the given subsets or printing the subset which satisfy the question conditions.

well i am trying to print all subset simple. its a general doubt dont relate it with question

Okay , Then you just have to traverse the array recursively and there can be two cases
1 . You pick the current element
2. You do not pick the current element

This simple code below explains that : output will contain all the subsets of the array

void printAllSubsets(vector<int>& arr , int n , vector<vector<int>>& output , int index , vector<int> curr){
// output will store all the subsets and we will add the subset to output when index == n (no more elements present) 
    if(index==n){
        output.push_back(curr); 
        return ; 
    }

    // does not include the present element 
    printAllSubsets(arr, n , output , index +1 , curr);
    // include the present element 
    curr.push_back(arr[index]); 
    printAllSubsets(arr , n , output , index+1 , curr); 
    curr.pop_back();
    return ; 
}

@anubhavb11 Hope it helps :blush: .
If your doubt is now resolved please mark it as resolved and rate me accordingly .
Else you can further questions here .
Thankyou

why we used curr.popback();
and how can i correct it in my code?

It is used because we don’t want to keep everything in our curr vector. It will work without it as well as while backtracking it will revert back to previous status of the curr vector.

Coming to your code , you were printing it wrong. you were printing from 0 to n instead print 0 to n-1 . it will work fine .

but i am not able to underastand your code yet we are using vector of vector its all getting too confusing

Like your code we are printing whenever we hit a base condition , in my code we are just storing every subset .
Since every subset is a vector then to store every vector you need a vector of vectors .

You can also just print the subsets instead of storing them in a 2d vector