#K82332. Subarray Sum Existence
Subarray Sum Existence
Subarray Sum Existence
You are given an array of n integers and a target integer k. Your task is to determine whether there exists a contiguous subarray whose sum is exactly k.
Formally, given an array A of size n, determine if there exist indices l and r (with 1 ≤ l ≤ r ≤ n) such that:
\(A[l] + A[l+1] + \cdots + A[r] = k\)
If such a subarray exists, output YES
. Otherwise, output NO
.
Example:
For n = 5
, A = [1, 2, 3, 4, 5]
and k = 15
, the entire array sums to 15, so the answer is YES
.
inputFormat
The input is given via standard input (stdin
) and has the following format:
- The first line contains two integers n and k, 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 elements of the array.
outputFormat
Print a single line to standard output (stdout
) containing YES
if there exists a contiguous subarray with sum equal to k, or NO
otherwise.
5 15
1 2 3 4 5
YES