#K95127. Longest Subarray with Sum k
Longest Subarray with Sum k
Longest Subarray with Sum k
Given an array of integers, find the length of the longest contiguous subarray whose elements sum up to a given integer k. In other words, find the maximum length l such that for some indices i and j with j - i + 1 = l, the following holds:
$$\sum_{t=i}^{j} a_t = k$$
If no such subarray exists, output 0.
It is guaranteed that the input is provided via STDIN and the answer should be printed to STDOUT.
inputFormat
The first line contains an integer n
(the number of elements in the array). The second line contains n
space-separated integers, representing the array. The third line contains an integer k
, the target sum.
Example:
5 1 -1 5 -2 3 3
outputFormat
Output a single integer representing the maximum length of a contiguous subarray that sums to k
.
Example:
4## sample
5
1 -1 5 -2 3
3
4