#C2110. Contiguous Subarray with Target Sum
Contiguous Subarray with Target Sum
Contiguous Subarray with Target Sum
You are given a non-negative integer array, an integer \(k\), and an integer \(S\). Your task is to determine whether there exists a contiguous subarray of length exactly \(k\) whose sum equals \(S\).
Note: If the length of the array is less than \(k\), you should output False
.
For example:
- Given array [1, 2, 3, 4, 5], \(k = 2\) and \(S = 3\), the answer is
True
because the subarray [1, 2] sums to 3. - Given array [1, 2, 3, 4, 5], \(k = 2\) and \(S = 10\), the answer is
False
because no contiguous subarray of length 2 sums to 10.
Solve the problem by reading input from stdin and printing the result to stdout. The result should be exactly either True
or False
(without quotes).
inputFormat
The input is given in the following format:
n a1 a2 a3 ... an k S
- The first line contains a single integer \(n\) which denotes the size of the array.
- The second line contains \(n\) space-separated non-negative integers representing the array elements.
- The third line contains two space-separated integers: \(k\) (the length of the contiguous subarray) and \(S\) (the target sum).
outputFormat
Output a single line containing either True
if there exists a contiguous subarray of length exactly \(k\) with sum equal to \(S\), or False
otherwise.
5
1 2 3 4 5
2 3
True
</p>