#K63492. Subarray with Given Sum

    ID: 31765 Type: Default 1000ms 256MiB

Subarray with Given Sum

Subarray with Given Sum

You are given an array of integers and a target sum \(S\). Your task is to determine if there exists a contiguous subarray of length at least 2 such that its sum is exactly equal to \(S\). Formally, you need to check whether there exist indices \(l\) and \(r\) (with \(r - l + 1 \ge 2\)) for which:

[ \sum_{i=l}^{r} a_i = S ]

If such a subarray exists, print YES; otherwise, print NO.

Note: It is recommended to use an efficient algorithm, for example using prefix sums and a hash map, in order to pass larger input sizes.

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 \(S\), where \(n\) is the size of the array and \(S\) is the desired sum.
  • The second line contains \(n\) space-separated integers representing the elements of the array.

All input is read from standard input (stdin).

outputFormat

For each test case, output a single line containing either YES if there exists a contiguous subarray of length at least 2 whose sum equals \(S\), or NO otherwise. The output for all test cases should be written to standard output (stdout), each answer on a new line.

## sample
1
5 12
1 2 3 7 5
YES