#K45087. Subarray with Given Sum

    ID: 27675 Type: Default 1000ms 256MiB

Subarray with Given Sum

Subarray with Given Sum

You are given an array of integers and a target integer \(S\). Your task is to determine whether there exists a contiguous subarray whose sum is exactly \(S\). This is a typical problem that can be solved using the prefix sum technique. In particular, if we let \(P(i)=a_0+a_1+\cdots+a_i\), then for any subarray \(a_{j+1}\ldots a_i\), its sum is \(P(i)-P(j)\). Thus, for each index, you may check if there is an earlier prefix sum such that \(P(i)-P(j)=S\).

If such a subarray exists, output YES; otherwise, output NO. Note that if the array is empty, the correct output is NO regardless of \(S\).

inputFormat

The first line of input contains an integer \(T\) representing the number of test cases. Each test case consists of two lines:

  1. The first line contains two integers \(N\) and \(S\) where \(N\) is the number of elements in the array and \(S\) is the target sum.
  2. The second line contains \(N\) space-separated integers denoting the array elements.

Note: If \(N=0\), the second line will be empty.

outputFormat

For each test case, output a single line containing YES if a contiguous subarray summing to \(S\) exists; otherwise, output NO.

## sample
3
5 12
1 2 3 7 5
5 15
1 2 3 4 5
3 9
7 1 2
YES

YES NO

</p>