#K38537. Subset Sum Problem
Subset Sum Problem
Subset Sum Problem
You are given an array of N integers and a target integer S. Your task is to determine whether there exists a subset of the array whose elements add up exactly to S. Formally, you need to decide if there exists a subset A' of the given set A such that
\(\sum_{a \in A'} a = S\)
If such a subset exists, print YES
; otherwise, print NO
. This problem is a classic example of the subset sum problem which is known to be NP-complete. Use an efficient approach (for example, dynamic programming) to solve the problem.
inputFormat
The input begins with an integer T denoting the number of test cases. Each test case consists of two lines:
- The first line contains two space-separated integers N and S, where N is the number of elements in the array and S is the target sum.
- The second line contains N space-separated integers representing the elements of the array.
outputFormat
For each test case, output a single line containing YES
if there exists a subset whose sum is exactly S, otherwise output NO
.
5
5 9
3 34 4 12 5
4 15
1 2 3 8
3 5
2 2 1
6 11
1 2 3 4 5 6
4 100
10 20 30 25
YES
NO
YES
YES
NO
</p>