View Code of Problem 133

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int cmp(const void *a,const void *b){
	return *(int *)a-*(int *)b;
}
int main() {
	int n,i,num,x,j,a[100000],low,high,f;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d %d",&num,&x);
		for(j=0;j<num;j++){
			scanf("%d",&a[j]);
		}
		qsort(a,num,sizeof(int),cmp);
		low=0;
		high=num-1;
		f=0;
		while(low<high){
			if(a[low]+a[high]==x){
				f=1;
				break;
			}
			if(a[low]+a[high]>x){
				high--;
			}
			if(a[low]+a[high]<x){
				low++;
			}
		}
		if(f==1){
			printf("YES\n"); 
		}else{
			printf("NO\n");
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 133