Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Minimum subsetsum difference (Dynamic Progrmming).cpp #395

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Programs/Minimum subsetsum difference (Dynamic Progrmming).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//Minimum subsetsum difference -> DP (Dynamic Progrmming)

#include<bits/stdc++.h>
using namespace std;
#define ll long long

void minsub(vector<ll>a)
{
ll n=a.size();
ll s=0;
for(ll i=0;i<n;++i)
{
s+=a[i];
}
ll k=(s)/2;
bool dp[n+1][k+1];
for(ll i=0;i<=n;++i) dp[i][0]=true;
for(ll i=1;i<=k;++i) dp[0][i]=false;

for(ll i=1;i<=n;++i)
{
for(ll j=1;j<=k;++j)
{
if(a[i-1]<=j)
{
dp[i][j]=dp[i-1][j-a[i-1]]|dp[i-1][j];
}
else
dp[i][j]=dp[i-1][j];
}
}
for(ll i=s/2;i>=0;--i)
{
if(dp[n][i]==true)
{
k=i;
break;
}
}
k=s-2*k;
cout<<"The Minimum Subset Difference of the given Array is : "<<k;
cout<<endl;
}
int main()
{
ll n;
cout<<"Enter the size of the Array : "<<endl;
cin>>n;
vector<ll>v;
ll a;
for(ll i=0;i<n;++i)
{
cin>>a;
v.push_back(a);
}
minsub(v);
//happy coding
}