View Code of Problem 133

import java.util.Arrays;
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 a[] = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = scanner.nextInt();
            }
            Arrays.sort(a);
            boolean flag = false;
            int p=0;
            int q=n-1;
            while (q != p) {
                int temp = a[p] + a[q];
                if (temp > X) {
                    q--;
                } else if (temp < X) {
                    p++;
                } else{
                    flag = true;
                    break;
                }
            }
            if (flag) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}

Double click to view unformatted code.


Back to problem 133