#K9786. Subarray Sum to \(x\)
Subarray Sum to \(x\)
Subarray Sum to (x)
You are given an array of \(n\) integers and an integer \(x\). Your task is to determine whether there exists at least one non-empty contiguous subarray whose elements sum exactly to \(x\). In other words, check if there exist indices \(l\) and \(r\) (with \(1 \leq l \leq r \leq n\)) such that:
\(a_l + a_{l+1} + \cdots + a_r = x\)
If such a subarray exists, you should print YES
, otherwise print NO
.
Note: The subarray must be contiguous and non-empty.
inputFormat
The first line contains two integers \(n\) and \(x\) separated by a space, where \(n\) is the number of elements in the array and \(x\) is the target sum.
The second line contains \(n\) space-separated integers representing the array elements.
\(1 \leq n \leq 10^5\) (for example) and the array elements as well as \(x\) can be positive, zero, or negative.
outputFormat
Output YES
if there exists a contiguous subarray that sums to \(x\); otherwise, output NO
.
5 9
1 2 3 4 5
YES