View Code of Problem 3686

import java.util.ArrayList;
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 money = scanner.nextInt();
			List<Integer> list = new ArrayList<Integer>();
			for(int j=0; j<n; j++) {
				list.add(scanner.nextInt());
			}
			boolean flag = false;
			Collections.sort(list);
			int a = 0,b = n-1;
			while(a<b) {
				if(list.get(a) + list.get(b) == money) {
					System.out.println("YES");
					flag = true;
					break;
				}else if(list.get(a) + list.get(b) < money) {
					a++;
				}else {
					b--;
				}
			}
			if(!flag) {
				System.out.println("NO");
			}
		}
		
		}
	}

Double click to view unformatted code.


Back to problem 3686