View Code of Problem 3686

#include<iostream> 
#include<algorithm>
using namespace std;
//二刷 
int main(){
	int T;
	scanf("%d",&T);
	while(T--) {
		int n, x;   
		scanf("%d %d", &n, &x); 
		int p[n]; //价钱 
		for(int i=0; i<n; i++){
			scanf("%d", &p[i]);
		}
		
		sort(p, p+n); //从小到大排序 
		
		int flag = 0;
		int low = 0, high = n-1;
		while(low < high) {  
			if(p[low] + p[high] == x){
				printf("YES\n");
				flag = 1;
				break;
			}
			else if(p[low] + p[high] < x){
				low++;
			}
			else high--;
		}
		if(flag == 0) printf("NO\n");
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3686