#K94127. Subset Sum with Unlimited Repetition
Subset Sum with Unlimited Repetition
Subset Sum with Unlimited Repetition
You are given T test cases. For each test case, you are given a set of integers and a target sum S. Your task is to determine whether it is possible to obtain the target sum by repeatedly using any of the given integers any number of times. In other words, check if there exists a sequence (with repetition allowed) of numbers from the set such that their sum is exactly S.
The problem can be formally stated as follows:
Given an integer \(S\) and an array \(\textbf{nums}\) of positive integers, determine if we can form \(S\) using a combination (repetitions allowed) of numbers in \(\textbf{nums}\).
Input for each test case:
- First line contains an integer
N
that denotes the number of available integers. - Second line contains
N
space-separated integers. - Third line contains the target sum
S
.
The first line of the input contains the number of test cases T
. For each test case, output "YES" if the target sum can be formed, otherwise output "NO". Each answer should be printed on a new line.
inputFormat
The first line contains an integer T
representing the number of test cases. For each test case, the format is as follows:
- The first line contains an integer
N
, the number of elements. - The second line contains
N
space-separated integers representing the list of numbers. - The third line contains an integer
S
, the target sum.
outputFormat
For each test case, output a single line containing either "YES" if the target sum can be formed using the given numbers (with unlimited usage), or "NO" otherwise.
## sample2
3
1 2 3
5
4
2 3 5 7
9
YES
YES
</p>