View Code of Problem 133

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
 
public class Main {
	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[] arr = new int[n];
			for(int j=0; j<n; j++) {
				arr[j] = scanner.nextInt();
			}
			Arrays.sort(arr);
			int a = 0;
			int b = n-1;
			boolean flag = false;
			while(a<b) {
				if(arr[a] + arr[b] == x) {
					System.out.println("YES");
					flag = true;
					break;
				}else if(arr[a] + arr[b]  < x) {
					a++;
				}else {
					b--;
				}
			}
			if(!flag) {
				System.out.println("NO");
			}
		}
		
	}
}

Double click to view unformatted code.


Back to problem 133