#K47807. Subset Sum Existence
Subset Sum Existence
Subset Sum Existence
You are given an array of n integers and a target integer k. Your task is to determine whether there exists a non-empty subset of the array whose elements sum exactly to k.
More formally, given an array A
, check if there exists a subset S \subseteq A
, with S \neq \emptyset
, such that:
$\sum_{x \in S} x = k$
If such a subset exists, output YES
; otherwise, output NO
.
The input consists of multiple test cases. For each test case, the first line contains two integers n
and k
. The next line contains n
space-separated integers. Process all test cases and output the answer for each on a separate line.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- The first line contains an integer
T
— the number of test cases. - For each test case:
- The first line contains two integers
n
(the number of elements in the array) andk
(the target sum). - The second line contains
n
space-separated integers representing the array.
- The first line contains two integers
outputFormat
For each test case, output a single line to standard output (stdout) containing YES
if there exists a non-empty subset that sums to k
, or NO
otherwise.
3
3 5
2 1 3
4 10
1 2 3 4
5 0
1 2 3 4 5
YES
YES
NO
</p>