#C5982. Subarray Sum Existence
Subarray Sum Existence
Subarray Sum Existence
Given an array of integers and a target sum \(t\), determine whether there exists a contiguous subarray whose sum is exactly equal to \(t\). If such a subarray exists, print "Yes"; otherwise, print "No".
The problem requires an efficient solution that can process large input sizes. A typical approach is to use prefix sums and a hash set to determine in \(O(n)\) time whether a subarray with the desired sum exists. Specifically, if we let \(S(i)\) denote the cumulative sum of the array up to the \(i\)-th element, then there exists a subarray from index \(j+1\) to \(i\) with sum equal to \(t\) if and only if \(S(i) - S(j) = t\). This can be checked quickly while iterating once through the array.
inputFormat
The first line contains two integers \(n\) and \(t\), where \(n\) is the number of elements in the array and \(t\) is the target sum.
The second line contains \(n\) space-separated integers representing the array elements.
outputFormat
Output a single line: "Yes" if there is at least one contiguous subarray with a sum exactly equal to \(t\); otherwise, print "No".
## sample5 5
1 2 3 4 5
Yes
</p>