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

Class Solution

Recursion/Homework/Subsets.cpp:11–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected