partition problem
#include <iostream>
#include<cstring>
using namespace std;
int main() {
int test ;
cin>> test ;
while (test > 0 )
{
int no_items ;
cin>>no_items;
int W[no_items];
int sum =0 ;
for (int i = 0 ; i<no_items ; i++ )
{
cin>>W[i];
sum = sum + W[i];
}
if (sum%2 !=0)
{
cout <<"NO"<<"\n";
test--;
continue ;
}
int A[sum/2];
memset(A,0,sizeof(A));
A[0]=1;
for (int j= sum/2; j>0; j--)
{
if (A[j]==W[0])
{
A[j] = 1 ;
}
}
for (int i = 1 ; i< no_items ; i++)
{
for (int j= sum/2 ; j>0 ; j--)
{
if(A[j]!=1)
{
if ( (j>=W[i]) && (A[j-W[i]]==1))
A[j]=1;
}
}
}
if (A[sum/2]==1)
cout <<"YES"<<"\n";
else
cout <<"NO"<<"\n";
test -- ;
}
return 0;
}
2
4
1 5 11 5
3
1 3 5
#include <iostream>
#include<cstring>
using namespace std;
int main() {
int test ;
cin>> test ;
while (test > 0 )
{
int no_items ;
cin>>no_items;
int W[no_items];
int sum =0 ;
for (int i = 0 ; i<no_items ; i++ )
{
cin>>W[i];
sum = sum + W[i];
}
if (sum%2 !=0)
{
cout <<"NO"<<"\n";
test--;
continue ;
}
int A[sum/2];
memset(A,0,sizeof(A));
A[0]=1;
for (int j= sum/2; j>0; j--)
{
if (A[j]==W[0])
{
A[j] = 1 ;
}
}
for (int i = 1 ; i< no_items ; i++)
{
for (int j= sum/2 ; j>0 ; j--)
{
if(A[j]!=1)
{
if ( (j>=W[i]) && (A[j-W[i]]==1))
A[j]=1;
}
}
}
if (A[sum/2]==1)
cout <<"YES"<<"\n";
else
cout <<"NO"<<"\n";
test -- ;
}
return 0;
}
2
4
1 5 11 5
3
1 3 5