View Code of Problem 133

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, x,temp;
		cin >> n >> x;
		vector<int>s;
		for (int i = 0; i < n; i++)
		{
			cin >> temp;
			s.push_back(temp);
		}
		sort(s.begin(), s.end());
		int p = 0, q = s.size()-1,flag=0;
		while (p < q) {
			if (s[p] + s[q] == x) {
				flag = 1;
				break;
			}
			else if (s[p] + s[q] < x) {
				p++;
			}
			else if (s[p] + s[q] > x) {
				q--;
			}
		}
		if (flag == 1)cout << "YES";
		else cout << "NO";
		if (t != 0)cout << endl;
	}
}

Double click to view unformatted code.


Back to problem 133