View Code of Problem 3686

#include<stdio.h>
#include<string.h>
#include<math.h>
int cmp(const void *a,const void *b) {
	return *(int*)a - *(int*)b;
}
int main() {
	int t,i,j,j1,tep;
	int n, money, a[100000],count,f;
	scanf("%d", &t);
	for (i = 0; i < t; i++) {
		count = 0;
		f = 0;
		scanf("%d %d", &n,&money);
		for (j = 0; j < n; j++) {
			scanf("%d", &a[j]);
		}
		qsort(a, n, sizeof(int), cmp);
	
		j = 0, j1 = n - 1;
		while (j < j1) {
			if (a[j] + a[j1] == money)
			{
				f = 1;
				break;
			}
			else if (a[j] + a[j1] > money){
				j1--;
			}
			else {
				j++;
			}
		}

		if (f == 1) {
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3686