#K86822. Find Subarray with Given Sum
Find Subarray with Given Sum
Find Subarray with Given Sum
You are given an array of integers and a target integer \(K\). Your task is to determine whether there exists a continuous subarray whose sum is exactly equal to \(K\). In other words, check if there exist indices \(l\) and \(r\) (with \(1 \leq l \leq r \leq N\)) such that
\[ \sum_{i=l}^{r} A_i = K \]
The input contains multiple test cases. For each test case, if such a subarray exists output YES
; otherwise, output NO
. A well-known approach to solve this problem efficiently is to use a prefix sum technique with a hash set. Note that the array may contain negative numbers, so simple two-pointer methods might not work in all cases.
inputFormat
The first line contains a single integer \(T\) representing the number of test cases.
For each test case, the first line contains 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\) integers \(A_1, A_2, \dots, A_N\) separated by spaces, representing the elements of the array.
Input is to be read from standard input (stdin).
outputFormat
For each test case, output a single line containing either YES
if there exists a continuous subarray whose sum equals \(K\), or NO
otherwise.
Output should be written to standard output (stdout).
## sample3
5 15
1 2 3 4 5
5 10
1 2 3 4 5
5 20
1 2 3 4 5
YES
YES
NO
</p>