#K44457. Subset Sum Problem
Subset Sum Problem
Subset Sum Problem
You are given an array of n integers and a target sum S. Your task is to determine whether there exists a subset of the given array whose sum is exactly equal to \(S\). This problem can be solved using a dynamic programming approach.
More formally, given an array \(a_1, a_2, \dots, a_n\) and a target \(S\), determine if there exists a subset \(I \subseteq \{1,2,\ldots,n\}\) such that \[ \sum_{i \in I} a_i = S. \]
If such a subset exists, print YES
; otherwise, print NO
.
inputFormat
The first line of the input contains an integer T denoting the number of test cases. Each test case consists of two lines:
- The first line contains two integers n and S, where n is the number of array elements and S is the target sum.
- The second line contains n space-separated integers representing the array elements.
It is guaranteed that T is at least 1 and that the input is given via standard input (stdin).
outputFormat
For each test case, output a single line containing YES
if there exists a subset with sum equal to S, or NO
otherwise. The output should be printed to standard output (stdout).
3
5 9
3 34 4 12 5
3 30
1 5 10
4 11
1 2 5 6
YES
NO
YES
</p>