#C898. Subarray Sum Existence
Subarray Sum Existence
Subarray Sum Existence
You are given an array of integers consisting of n elements and an integer k. Your task is to determine whether there exists a contiguous subarray whose sum is exactly equal to k.
A subarray is defined as a sequence of consecutive elements in the array. In mathematical terms, for some indices \(i\) and \(j\) with \(1 \leq i \leq j \leq n\), the subarray \(a_i, a_{i+1}, \dots, a_j\) is considered valid if:
\(\sum_{t=i}^{j} a_t = k\)
Print YES
if such a subarray exists, otherwise print NO
. Consider special cases including arrays with negative numbers, and even an empty array.
inputFormat
The input is given via stdin in the following format:
- The first line contains two integers n and k separated by a space, where n is the number of elements in the array and k is the target sum.
- The second line contains n space-separated integers representing the array elements. If n is 0, the second line will be empty.
outputFormat
Output a single line to stdout containing YES
if there exists any contiguous subarray with a sum equal to k, otherwise output NO
.## sample
5 9
1 2 3 4 5
YES