Saturday, 26 August 2017

Given an array A[] of n numbers and another number x, determine whether or not there exist two elements in A whose sum is exactly x.

Input: The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N and X,N is the 
size of array.
The second line of each test case contains N integers representing array elements C[i].

Output:
Print "Yes" if there exist two elements in A whose  sum is exactly x, else "No" without quotes.

Constraints: 1 ≤ T ≤ 100
1 ≤ N ≤ 200
1 ≤ C[i] ≤ 1000

Example:
 
Input:
2
6 16
1 4 45 6 10 8
5 10
1 2 4 3 6  
Output:
Yes
 
Yes
                                         C-Solution
 
#include <stdio.h>

int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        int n,x,i,j,flag=0;
        scanf("%d %d",&n,&x);
        int a[n];
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(a[i]+a[j]==x)
                    flag=1;
            }
        }
        if(flag==1)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

No comments:

Post a Comment