forked from NandeeshG/moonwalking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsetSum.cpp
More file actions
100 lines (86 loc) · 2.01 KB
/
Copy pathsubsetSum.cpp
File metadata and controls
100 lines (86 loc) · 2.01 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// given an array and k
// return all subsets summing to k
//naman ed
#include <iostream>
#include <math.h>
#define debug(a) cout << #a << '=' << a << endl
using namespace std;
int subsetSum(int size, int *arr, int k, int **output, int sets, int *temp)
{
//base case
if (size == 0)
{
//calc sum of temp
int sum = 0;
for (int i = 1; i <= temp[0]; ++i)
{
sum += temp[i];
}
// check given condition
if (sum == k)
{
//add temp to output
output[sets][0] = temp[0];
for (int i = 1; i <= temp[0]; ++i)
{
output[sets][i] = temp[i];
}
return sets + 1;
}
else
{
return sets;
}
}
//don't put current element to temp
int ans1 = subsetSum(size - 1, arr + 1, k, output, sets, temp);
//put current elem to temp
//temp[++temp[0]] = arr[0];
temp[temp[0] + 1] = arr[0];
++temp[0];
int ans2 = subsetSum(size - 1, arr + 1, k, output, ans1, temp);
return ans1 + ans2;
}
int subsetSum(int size, int *arr, int k, int **output)
{
int *temp = new int[size + 1];
temp[0] = 0;
return subsetSum(size, arr, k, output, /*sets = */ 0, temp);
}
int main()
{
int size, k;
cin >> size;
int *arr = new int[size];
for (int i = 0; i < size; ++i)
{
cin >> arr[i];
}
cin >> k;
int **output;
int pow2 = pow(2, size);
output = new int *[pow2];
for (int i = 0; i < pow2; ++i)
{
output[i] = new int[size + 1];
}
int ans = subsetSum(size, arr, k, output);
debug(ans);
cout << "\nSolution sets-\n";
for (int i = 0; i < ans; ++i)
{
for (int j = 1; j <= output[i][0]; ++j)
{
cout << output[i][j] << ' ';
}
cout << endl;
}
cout << "These all sum to k.\n";
delete[] arr;
for (int i = 0; i < pow2; ++i)
{
delete[] output[i];
}
delete[] output;
return 0;
}