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

Subset Sum problem #64

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions Data Structure and Algorithm/DP/Equal Sum Partition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Problem Statement:
// Given an array arr[] of size N,
// check if it can be partitioned into two parts such that the sum of elements in both parts is the same.
// OUTPUT:if the array can be partitioned into two equal sum parts print
// YES else NO

#include <bits/stdc++.h>
#define int long long
//AND THE SHOW BEGINS :)//
using namespace std;
int dp[60005][500];
bool check(int current,int sum,int arr[],int n)
{
if(n==0) return false;
if(dp[current][n-1]!=-1) return dp[current][n-1];
else if(current+arr[n-1]==sum) return dp[current][n-1]=1;
else if(current+arr[n-1]<sum)
{
return dp[current][n-1]=((check(current+arr[n-1],sum,arr,n-1)) || (check(current,sum,arr,n-1)));
}
else
{
return dp[current][n-1]=check(current,sum,arr,n-1);
}
}
signed main()
{
int t=1;
cin>>t;
while(t--)
{
int n;cin>>n;
int arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
int sum=0;
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++) sum+=arr[i];
if(sum%2!=0) return 0;
else
{
sum/=2;
int current=0;
if(check(current,sum,arr,n)) cout<<"YES"<<"\n";
else cout<<"NO"<<"\n";
}
}
return 0;
}


44 changes: 44 additions & 0 deletions Data Structure and Algorithm/DP/Subset Sum problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Problem Statement:
// Given an array of non-negative integers, and a value sum,
// determine if there is a subset of the given set with sum equal to given sum.
// Output:if such subset exists print 1 else 0;

#include <bits/stdc++.h>
#define int long long
//AND THE SHOW BEGINS :)//
using namespace std;
int dp[10005][10005];
bool check(int current, int sum, int arr[], int n)
{
if (n == 0)
return false;
if (dp[current][n - 1] != -1)
return dp[current][n - 1];
else if (current + arr[n - 1] == sum)
return dp[current][n - 1] = 1;
else if (current + arr[n - 1] < sum)
{
return dp[current][n - 1] = ((check(current + arr[n - 1], sum, arr, n - 1)) || (check(current, sum, arr, n - 1)));
}
else
{
return dp[current][n - 1] = check(current, sum, arr, n - 1);
}
}
signed main()
{
int t = 1;
cin >> t;
while (t--)
{
int n, sum;
cin >> n >> sum;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
int current = 0;
memset(dp, -1, sizeof(dp));
cout << check(current, sum, arr, n);
}
return 0;
}
14 changes: 11 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,25 @@ const participants = [
"linkedIn": "https://www.linkedin.com/in/sachin-choudhary-7037381a9/",
"github": "https://github.com/sachin-611",
},
{ "name": "Ankit Gadhwal",
{
"name": "Ankit Gadhwal",
"img": "https://avatars.githubusercontent.com/u/60103160?v=4",
"year": "BE - 2023",
"linkedIn": "https://www.linkedin.com/in/ankit-gadhwal-a9451b18b/",
"github": "https://github.com/Ankit-123-123",
}
},
{
"name": "Shivangi Chauhan",
"img": "https://avatars.githubusercontent.com/u/62792560?v=4",
"year": "BE - 2023",
"linkedIn": "https://www.linkedin.com/in/shivangi-chauhan-57919a191/",
"github": "https://github.com/Flaming-Phoenix24",
},
]

function loadParticipants() {
participants.forEach((participant) => {


const profileCard = `
<div class="box">
Expand Down