#C8198. Contiguous Subarray Zero Sum
Contiguous Subarray Zero Sum
Contiguous Subarray Zero Sum
You are given an array of n integers and an integer k. Your task is to determine whether there exists a contiguous subarray of length k whose elements sum to zero. In other words, check if there is an index i (with 0 ≤ i ≤ n - k) such that
\(\sum_{j=i}^{i+k-1}a_j = 0\)
If such a subarray exists, output YES
; otherwise, output NO
.
Note: It is guaranteed that \(1 \leq k \leq n\).
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains two space-separated integers n and k, where \(1 \leq k \leq n\).
- The second line contains n integers separated by spaces representing the array elements.
outputFormat
Output a single line to stdout containing YES
if there exists a contiguous subarray of length \(k\) with a sum of zero; otherwise, output NO
.
7 3
1 -2 1 2 -2 3 1
YES