#K6201. Contiguous Subarray with Exact Sum
Contiguous Subarray with Exact Sum
Contiguous Subarray with Exact Sum
You are given an array of integers and two integers k and target. Your task is to determine whether there exists a contiguous subarray of length exactly k whose elements sum to target.
It is guaranteed that the array can have both positive and negative integers. The problem can be formulated using a sliding window technique. Specifically, if we denote by \(A\) the given array, you need to decide if there exists some index \(i\) such that:
[ \sum_{j=i}^{i+k-1} A_j = target ]
If such a subarray exists, output YES
, otherwise output NO
.
inputFormat
The input is read from standard input and consists of three lines:
- The first line contains an integer \(n\) which is the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
- The third line contains two space-separated integers \(k\) and \(target\), where \(k\) is the length of the subarray to consider and \(target\) is the required sum.
outputFormat
Print a single line to standard output. The output should be YES
if there exists a contiguous subarray of length exactly \(k\) whose sum equals \(target\); otherwise, print NO
.
6
1 2 3 4 5 6
3 6
YES