forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubset Sums.cpp
55 lines (47 loc) · 1.12 KB
/
Subset Sums.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Subset Sums
===========
Given a list(Arr) of N integers, print sums of all subsets in it. Output should be printed in increasing order of sums.
Example 1:
Input:
N = 2
Arr = [2, 3]
Output:
0 2 3 5
Explanation:
When no elements is taken then Sum = 0.
When only 2 is taken then Sum = 2.
When only 3 is taken then Sum = 3.
When element 2 and 3 are taken then
Sum = 2+3 = 5.
Example 2:
Input:
N = 3
Arr = [5, 2, 1]
Output:
0 1 2 3 5 6 7 8
Your Task:
You don't need to read input or print anything. Your task is to complete the function subsetSum() which takes a list/vector and an integer N as an input parameter and return the list/vector of all the subset sums in increasing order.
Expected Time Complexity: O(2N)
Expected Auxiliary Space: O(2N)
Constraints:
1 <= N <= 15
0 <= Arr[i] <= 10000
*/
void dfs(vector<int> &arr, int i, vector<int> &ans, int stn)
{
if (i == arr.size())
{
ans.push_back(stn);
return;
}
dfs(arr, i + 1, ans, stn);
dfs(arr, i + 1, ans, stn + arr[i]);
}
vector<int> subsetSums(vector<int> arr, int N)
{
vector<int> ans;
dfs(arr, 0, ans, 0);
sort(ans.begin(), ans.end());
return ans;
}