#C11657. Maximum Length Subarray with Exact Sum
Maximum Length Subarray with Exact Sum
Maximum Length Subarray with Exact Sum
John is given an array of integers and a target integer (k). He needs to find a contiguous subarray whose elements sum exactly to (k) and among all such subarrays, the one with the maximum length. If no such subarray exists, print (-1). Note that the indices in the output should be 1-indexed.
For example, given the array [2, 1, 3, 4, 5] and (k=10), the subarray from index 1 to 4 (i.e. [2, 1, 3, 4]) sums to 10.
inputFormat
The input is read from standard input (stdin) and consists of three lines:
1. The first line contains a single integer (n) denoting the number of elements in the array.
2. The second line contains (n) space-separated integers representing the array elements.
3. The third line contains the integer (k), the target sum.
outputFormat
The output should be printed to standard output (stdout). If a valid subarray exists, print two space-separated integers representing the starting and ending indices (1-indexed) of the subarray with the maximum length such that the sum of its elements is exactly (k). Otherwise, print (-1).## sample
5
2 1 3 4 5
10
1 4
</p>