View Code of Problem 133

import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
	public static boolean heIsX(int[] a,int x) {
		int low = 0; 
		int high = a.length - 1;
		while(low < high) {
			if(a[low] + a[high] < x) {
				low++;
			}
			if(a[low] + a[high] > x) {
				high--;
			}
			if(a[low] + a[high] == x) {
				return true;
			}
		}
		return false;
	}
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int t = scanner.nextInt();
		for(int i = 0;i < t;i++) {
			int n = scanner.nextInt();
			int x = scanner.nextInt();
			int[] a = new int[n];
			for(int j = 0;j < n;j++) {
				a[j] = scanner.nextInt();
			}
			Arrays.sort(a);
			if(heIsX(a, x)) {
				System.out.println("YES");
			}
			else {
				System.out.println("NO");
			}
		}
	}
}

Double click to view unformatted code.


Back to problem 133