[InterviewBit] Minimum Difference Subsets!

Minimum Difference Subsets!

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Solution::solve(vector<int> &A) {
int sum = 0;
for(auto a : A) sum += a;
vector<int> dp((sum + 1) / 2 + 1,0);
dp[0] = 1;
int res = sum;
for(auto a : A) {
for(int j = dp.size() - 1; j >= a; j--) {
if(dp[j-a]) dp[j] = 1;
if(dp[j]) res = min(res, abs(sum - 2 * j));
}
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/08/PS/Codeforces/minimum-difference-subsets/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.