#C8595. Circular Subarray Sum
Circular Subarray Sum
Circular Subarray Sum
You are given a circular array of integers. In a circular array, the element following the last element is the first element. Your task is to determine whether there exists a contiguous subarray (which may wrap around from the end back to the beginning) whose elements sum up to a given target value ( k ). The length of the subarray must be at most ( n ), the size of the array.
For example, given the array [3, 1, -2, 5, 4] and ( k=7 ), a valid subarray is [5, 4, 3, -2] (wrapping around), which sums to 10? (Check carefully!) Actually, a better example is to notice that some subarray exists which, when summed, equals ( k ). Your solution should output "YES" if such a subarray exists and "NO" otherwise.
inputFormat
The first line contains two integers ( n ) and ( k ), where ( n ) ((1 \le n \le 10^5)) is the number of elements in the array, and ( k ) ((|k| \le 10^9)) is the target sum. The second line contains ( n ) space-separated integers representing the elements of the array.
outputFormat
Output a single line containing "YES" if there exists a contiguous subarray (considering the circular nature of the array) whose sum equals ( k ), and "NO" otherwise.## sample
5 7
3 1 -2 5 4
YES