#C11020. Subarray Sum Existence
Subarray Sum Existence
Subarray Sum Existence
You are given an array of non-negative integers and a target sum k. Your task is to determine whether there exists a contiguous subarray whose sum is exactly k.
The efficient solution to this problem uses a sliding window technique. In this method, we maintain a window with indices \(start\) and \(end\) and a running sum \(S = a_{start} + a_{start+1} + \cdots + a_{end}\). We adjust this window so that \(S\) approaches the target value \(k\). If at any point \(S = k\), output True
; otherwise, after processing the array, output False
.
Note: When the array is empty, the output should be False
.
inputFormat
The input is read from standard input (stdin
) and consists of two lines:
- 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 non-negative integers separated by space representing the array. If n is 0, the second line will be empty.
outputFormat
Output a single line to standard output (stdout
) containing either True
if there exists a contiguous subarray whose sum is exactly k, or False
if no such subarray exists.
5 12
1 2 3 7 5
True