This is my code for Minimum Sum Partition using DP.
#include<bits/stdc++.h>
using namespace std;
int minPart(vector<int>v)
{
int sum = accumulate(v.begin(),v.end(),0);
bool dp[v.size()+1][sum+1];
fill(*dp,*dp+(v.size()+1)*(sum+1),false);
for(int i=0;i<=v.size();i++)
{
dp[i][0]=true;
for(int j=1;i>0 && j<=sum;j++)
{
dp[i][j]=dp[i-1][j];
if(v[i-1]<=j)
dp[i][j] |= dp[i-1][j-v[i-1]];
}
}
int j = sum/2;
while(j>=0 && !dp[v.size()][j]){
j--;
}
return sum-2*j;
}
int main()
{
vector<int> v = {10,20,15,5,25};
cout<<minPart(v)<<endl;
return 0;
}
I also want to find the subset's which result in the minimum difference of sum.
For Example for the set {1,6,11,5}, the min difference is 1 and the subset's are {1,5,6} {11}. I want to print both the subset's {1,5,6},{11} .
Is there any efficient way to find print both the subset's which result in the min sum difference?