#K46492. Contiguous Subarray Sum Equals K
Contiguous Subarray Sum Equals K
Contiguous Subarray Sum Equals K
You are given an array of integers and an integer \( k \). Your task is to determine whether there exists a contiguous subarray whose elements sum up to \( k \). Formally, given an array \( A = [a_1, a_2, \dots, a_n] \), determine if there exist indices \( i \) and \( j \) with \( 1 \le i \le j \le n \) such that
\[ \sum_{p=i}^{j} a_p = k \]
If such a subarray exists, print true
; otherwise, print false
.
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains an integer \( n \) denoting the number of elements in the array.
- The second line contains \( n \) space-separated integers representing the elements of the array. If \( n = 0 \), this line will be empty.
- The third line contains an integer \( k \), the target sum.
outputFormat
Print to standard output (stdout) a single line with true
if there exists a contiguous subarray whose sum is equal to \( k \); otherwise, print false
.
5
1 2 3 4 5
9
true