View Code of Problem 3686

#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX 1000000+10
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,x;
		scanf("%d%d",&n,&x);
		int a[n];
		int i,j;
		int flag=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		for(i=1;i<n;i++)
		{
			int t=a[i];
			for(j=i-1;j>=0&&a[j]>t;j--)
			{
				a[j+1]=a[j];
			}
			a[j+1]=t;
		}
		int low=0,high=n-1;
		while(low<high)
		{
			if(a[low]+a[high]>x) high--;
			else if(a[low]+a[high]<x) low++;
			else if(a[low]+a[high]==x){
				flag=1;
				break;
			}
		}
		if(flag==1) printf("YES\n");
		else printf("NO\n");
	}

}

Double click to view unformatted code.


Back to problem 3686