ws-2.2 20bcs9740

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment: 2.

Student Name: Naitik onga UID:20BCS9740


Branch: BE-CSE Section/Group: 804 A
Semester:5th Subject Code:20CSP-312
Subject Name: DAA Lab Date of Performance:31-10-2022

1. Aim/Overview of the practical:


To implement a subset-sum problem using dynamic programming

2. Source Code:

#include<bits/stdc++.h>
using namespace std;
void solve(int a[], int n, int sum)
{
int i,j;

int dp[n+1][sum+1];

for(i=0;i<=n;i++)
dp[i][0]=1;

for(j=0;j<=n;j++)
dp[0][j]=0;

for(i=1;i<=n;i++)

{
for(j=1;j<=sum;j++)
{
if(j<a[i])
dp[i][j]=dp[i-1][j];

else
dp[i][j]=dp[i-1][j] || dp[i-1][j-a[i]];
}
}

if(dp[n][sum]==1)
cout<<"Possible"<<endl;

else
cout<<"Not Possible"<<endl;
}
int main()
{
int n;
cin>>n;

int a[n];

for(long i=0;i<n;i++)
{
cin>>a[i];
}

int sum;
cin>>sum;

solve(a,n,sum);
}

\
3. Result/Output:

You might also like