Saturday 4 February 2012

SPOJ (ONLINE JUDGE) CODING CONTEST PROBLEM



SPOJ CONTEST PROBLEM

Half of a Set

Problem code: HS11PART

You are given X, a set of n < 20 positive integers: x1x2, ... xn, where xi < 20. Let S=x1 +x2 + ... + xn be the sum of all xi. Please, check if there exists a subset of X whose sum of elements is equal to S/2.

Input

First t < 500, the nuber of sets. Next, for each test case, two lines follow. The first contains n, while the second the n set elements, separated by spaces.

Output

For each test case output one word in a separate line: YES if it is possible to achieve S/2 and NO if it is impossible.

SO I HAVE WRITTEN THIS SIMPLE CODE FOR YOU....

#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<list>
#define show(x) copy(x.begin(),x.end(),output)
#include<iterator>
#define sort(x) sort(x.begin(),x.end())
#include<string>
#define vs vector<string>
#define vi vector<int>
#define pb push_back
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    int S = 0;
    int t = 0 ; cin>>t;
    int j = 0;
    int X[20];
    int flag = 0;
    for(int i = 0 ; i < t ; i ++)
    {
            int n ; cin>>n;
            for(j = 0 ; j < n ; j++)
            {
                 scanf("%d",&X[j]);
                    
            }
            S = 0;
            for(j = 0 ; j < n ; j++)
            {
                    S = S + X[j];
            }
            flag = 0;
            for(j = 0 ; j < n-1 ; j++)
            {
                  for(int k = j+1; k < n ; k++)
                  {
                          
                     if( (X[j] + X[k]) == S/2 )
                     {
                         flag++;
                         break;
                     }
                     if( flag != 0)
                     {
                         break;
                     }
                  }
            }
            if( flag != 0 )
            {
                printf("YES\n");
                
            }
            else
            {
                printf("NO\n");
            }
    }
    
    return 0;
}
                     
            
 BY : SAURABH BHATIA (GNDU , RC , JAL)            




No comments:

Post a Comment