-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum sum partition.cpp
82 lines (61 loc) · 1.36 KB
/
Minimum sum partition.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
74
75
76
77
78
79
80
81
82
PROBLEM:
Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an
integer 'N' denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the
array.
Output:
In each seperate line print minimum absolute difference.
Constraints:
1<=T<=30
1<=N<=50
1<=A[I]<=50
Example:
Input:
2
4
1 6 5 11
4
36 7 46 40
Output :
1
23
SOLUTION:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,i,j,s=0,ans=INT_MAX;
cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
s+=a[i];
bool dp[s+1]; //dp[i] represent that sum i can be made or not
for(i=1;i<=s;i++)
dp[i]=false;
dp[0]=true;
for(i=0;i<n;i++)
{
int curr=a[i];
for(j=s;j>=a[i];j--)
{
if(dp[j]==true || dp[j-curr]==false)
continue;
else
dp[j]=true;
}
}
for(i=0;i<s;i++)
{
if(dp[i]==true)
ans=min(ans,abs(i-(s-i)));
}
cout<<ans<<endl;
}
return 0;
}