forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78.cpp
More file actions
36 lines (32 loc) · 1.13 KB
/
78.cpp
File metadata and controls
36 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
//f will take the input array, the index or the position of the element on which we need to take the decision and the result vector of vectors
void helper(vector<int>&nums, int startIndex, vector<vector<int>>& res){
int n= nums.size();
//base case
if (startIndex==n){
res.push_back({});
return;
}
//f(ar,startIndex)= f(ar,startIndex+1) U {nums[startIndex] U f(ar.startIndex+1)}
vector<vector<int>> subSol;
helper(nums, startIndex+1, res);
//exclude the startIndex
subSol= res;
//include the startIndex
vector<int> temp;
for (int i=0;i<subSol.size();i++){
temp= subSol[i];
temp.push_back(nums[startIndex]);
res.push_back(temp);
}
}
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
helper(nums,0,res);
return res;
}
};
//=================================
//Time complexity for the above algorithm asymptotically: O(2^n) where n is the size of the input array
//Space complexity: O(n)