Skip to content

Commit

Permalink
dp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish-kumar7 committed Mar 2, 2020
1 parent 796c627 commit 0d8420c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions DP/Partition Problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<bits/stdc++.h>
using namespace std;

int dp[101][100001];
int checker[101][100001]={0};

bool partition(int A[],int n,int i,int sum){
if(sum==0){
return 1;
}
if(i==n && sum!=0){
return 0;
}
if(sum<0){
return 0;
}
if(checker[i+1][sum]==0){
checker[i+1][sum]=1;
dp[i+1][sum]=partition(A,n, i+1, sum);
}
if(checker[i+1][sum-A[i]]==0){
checker[i+1][sum-A[i]]=1;
dp[i+1][sum-A[i]]=partition(A,n, i+1, sum-A[i]);
}
return ( dp[i+1][sum] || dp[i+1][sum-A[i]] ) ;
}

int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int A[n];
int sum=0;
for(int i=0;i<n;i++){
cin>>A[i];
sum+=A[i];
}
if(sum%2!=0){
cout<<"NO"<<endl;
continue;
}
sum=sum/2;
if(partition(A,n,0,sum)==1){
cout<<"YES"<<endl;
}
else {
cout<<"NO"<<endl;
}
}
return 0;
}

0 comments on commit 0d8420c

Please sign in to comment.