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;
            for (int j = 0; j < n; j++) {
                if (a[j] >= X||flag) {
                    break;
                }
                for (int k = i; k < n; k++) {
                    if (a[j] + a[k] == X) {
                        flag = true;
                        break;
                    }else if (a[j]+a[k]>X){
                        break;
                    }
                }
            }
            if (flag) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }
}

Double click to view unformatted code.


Back to problem 133