View Code of Problem 3686

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,x;
		cin>>n>>x;
		int a[n];
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		int flag=0;
		sort(a,a+n);
		int l=0,h=n-1;
		while(l<h)
		{
			if(a[l]+a[h]<x) l++;
			else if(a[l]+a[h]>x) h--;
			else
			{
				flag=1;
				break;
			}
		}
/*		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				if(a[i]+a[j]==x)
				{
					flag=1;
					break;
				}
			}
		}
	*/
		if(flag==1)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3686