#K46082. Subset Sum Problem
Subset Sum Problem
Subset Sum Problem
You are given an array of integers and a target sum \( k \). Your task is to determine whether there exists a subset of the array whose elements sum exactly to \( k \). Note that the empty subset is considered to have a sum of 0. In other words, if \( k = 0 \) and the array is empty, the answer should be "YES".
The problem is a classic application of dynamic programming. You can use a 2-dimensional DP table where \( dp[i][j] \) indicates whether a subset of the first \( i \) numbers can sum up to \( j \). The transition involves choosing whether or not to include the current element.
inputFormat
The first line of input consists of two integers \( n \) and \( k \), where \( n \) is the number of elements in the array and \( k \) is the target sum.
The second line contains \( n \) space-separated integers representing the elements of the array. If \( n = 0 \), the second line will be empty.
outputFormat
Output a single line containing "YES" if there exists a subset whose sum equals \( k \), otherwise output "NO".
## sample5 9
3 34 4 12 5
YES
</p>