-
Notifications
You must be signed in to change notification settings - Fork 3
/
min subset sum.cpp
73 lines (69 loc) · 1.59 KB
/
min subset sum.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
#define INT_MAX 1e+9
using namespace std;
/*
void print(vector <int> const &a) {
cout << "The vector elements are : ";
for(int i=0; i < a.size(); i++)
cout << a.at(i) << ' ';
}
*/
int minsubsetsum(int arr[], int sum, int n) {
bool t[n + 1][sum + 1];
memset(t, false, sizeof(t));
for(int i = 0; i < n +1; i++) {
for(int j = 0; j < sum + 1; j++) {
if(i == 0 && j == 0)
t[i][j] = 1;
else if(j == 0)
t[i][j] = 1;
else if(i == 0)
t[i][j] = 0;
}
}
for(int i = 0; i < n + 1; i++) {
for(int j = 0; j < sum + 1; j++) {
if(arr[i-1] <= sum)
t[i][j] = t[i-1][j - arr[i-1]] || t[i-1][j];
else
t[i][j] = t[i-1][j];
}
}
/*
vector<int> nums;
for(int j = 0; j < (sum/2); j++) {
if(t[n][j] == true)
nums.push_back(t[n][j]);
}
print(nums);
int mn = INT_MAX;
for(int i = nums.begin(); i != nums.end(); ++i) {
if(*i < mn)
mn = *i;
}
return mn;
*/
int diff = INT_MAX;
for (int j=sum/2; j>=0; j--) {
if (t[n][j] == true) {
diff = sum-2*j;
break;
}
}
return diff;
}
int main() {
int t, n, sum = 0;
cin >> t;
while(t--) {
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
for(int i = 0; i < n; i++) {
sum += arr[i];
}
cout << minsubsetsum(arr, sum, n) << endl;
}
}