View Code of Problem 133

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Integer get = Integer.valueOf(scanner.nextLine());
        for (int i = 0; i < get; i++) {
            int[] input = Arrays.stream(scanner.nextLine().split(" ")).mapToInt((String s) -> {
                return Integer.valueOf(s);
            }).toArray();
            int n = input[0];
            int x = input[1];
            int[] gets = Arrays.stream(scanner.nextLine().split(" ")).mapToInt((String s) -> {
                return Integer.valueOf(s);
            }).toArray();
            Arrays.sort(gets);
            int left = 0;
            int right = gets.length - 1;
            while (left < right) {
                if (gets[left] + gets[right] == x) {
                    System.out.println("YES");
                    break;
                } else if (gets[left] + gets[right] > x) {
                    right--;
                } else if (gets[left] + gets[right] < x) {
                    left++;
                }
            }
            if (right <= left) {
                System.out.println("NO");
            }
        }
    }
}

Double click to view unformatted code.


Back to problem 133