View Code of Problem 133

#include<stdio.h>
#include<stdlib.h>

int cmp(const void * a, const void * b)
{
	return *(int *)a - *(int *)b;
}

int main()
{
	int T = 0;
	scanf("%d", &T);
	while (T-- > 0)
	{
		int n = 0, x = 0; 
		scanf("%d %d", &n, &x);
		int* a = (int *)malloc(sizeof(int) * n);
		for (int i = 0; i < n; ++i)
			scanf("%d", &a[i]);
		qsort(a, n, sizeof(a[0]), cmp);
		int tag = 0;
		int low = 0, high = n - 1;
		while (low < high)
		{
			if (a[low] + a[high] < x) ++low;
			else if (a[low] + a[high] > x) --high;
			else
			{
				printf("YES\n");
				tag = 1;
				break;
			}
		}
		if (tag == 0)
			printf("NO\n");
	}
}

Double click to view unformatted code.


Back to problem 133