#C6502. Three Sum Problem: Find Triplet with Sum K

    ID: 50270 Type: Default 1000ms 256MiB

Three Sum Problem: Find Triplet with Sum K

Three Sum Problem: Find Triplet with Sum K

You are given an array of n integers and an integer k. Your task is to determine whether there exist three distinct elements in the array whose sum is exactly k. Formally, you need to check if there exist indices i, j, and l with i \lt j \lt l such that:

\(a_i + a_j + a_l = k\)

The input will be read from stdin and the answer should be printed to stdout with one result per test case. A result of "YES" should be printed if such a triplet exists, otherwise print "NO".

This problem is designed to test your ability to implement efficient two-pointer or sorting based solutions for the classic three sum problem. Use appropriate algorithms to ensure your solution works within the given constraints.

inputFormat

The first line contains an integer t (1 \le t \le 10), the number of test cases. Each test case is described as follows:

  • The first line contains two integers n and k (3 \le n \le 105, -109 \le k \le 109), where n is the number of elements in the array and k is the target sum.
  • The second line contains n space-separated integers \(a_1, a_2, \dots, a_n\) (each |ai| \le 109).

Input is given via standard input (stdin).

outputFormat

For each test case, output a single line containing "YES" if there exists a triplet of distinct elements that sums to k, otherwise output "NO". The output should be written to standard output (stdout).

## sample
3
5 9
1 2 3 4 5
3 10
3 3 3
4 0
-1 2 -3 4
YES

NO YES

</p>