#K79077. Subarray with Given Sum

    ID: 35228 Type: Default 1000ms 256MiB

Subarray with Given Sum

Subarray with Given Sum

Given an array of integers \(a_1, a_2, \ldots, a_N\) and a target sum \(S\), determine whether there exists a contiguous subarray whose sum is exactly \(S\). More formally, check if there exist indices \(1 \leq i \leq j \leq N\) such that

[ \sum_{k=i}^{j} a_k = S ]

Output YES if such a subarray exists, otherwise output NO.

Constraints:

  • The array elements and \(S\) are integers.
  • The size of the array, \(N\), and the number of test cases are such that a solution with optimal time complexity is required.

Note: The input consists of multiple test cases.

inputFormat

The first line contains a single integer \(T\), the number of test cases. Each test case consists of two lines:

  1. The first line contains two space-separated 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 representing the array \(a_1, a_2, \ldots, a_N\).

outputFormat

For each test case, output a single line containing either YES if there exists a contiguous subarray whose sum is exactly \(S\) or NO otherwise.

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

</p>