MCPcopy Create free account
hub / github.com/chaharnishant11/CodeIn10DSA / Solution

Class Solution

Recursion/Code/subsets.cpp:7–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5using namespace std;
6
7class Solution{
8
9 public:
10
11 vector<vector<int> > ans; //Global Variable
12 //vector is a Dynamic Data structure similar to array but unlike array we do not need to declare its size in advance
13
14 void helper(vector <int> A,vector<int>temp, int i){
15 if (i==A.size()){ //Base Case
16 ans.push_back(temp); //this will keep the contents of temp in ans for now
17 return;
18 }
19
20 temp.push_back(A[i]); //Appends or adds element at the end
21 helper(A,temp,i+1); //Inclusion or Take condition
22
23 temp.pop_back(); //Removes an element from the end
24 helper(A,temp,i+1); //Exclusion or not-take condition
25
26 return;
27 }
28
29 vector<vector<int> > subsets(vector<int>& A)
30 {
31 //code here
32 vector<int> temp;
33 helper(A,temp,0);
34 sort(ans.begin(),ans.end()); //this is to sort the array
35 return ans;
36 }
37
38};
39
40
41// { Driver Code Starts.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected