#C12432. Longest Contiguous Subarray with Given Sum
Longest Contiguous Subarray with Given Sum
Longest Contiguous Subarray with Given Sum
Given an array of integers, your task is to find the longest contiguous subarray whose sum is equal to a given target value \(T\). If there is no such subarray, output an empty line.
Formally, given an array \(A = [a_1, a_2, \dots, a_n]\) and a target \(T\), find the contiguous segment \(A[i \dots j]\) such that \(\sum_{k=i}^{j} a_k = T\) and \(j-i+1\) is maximized. If multiple answers exist, output the one that appears first. If no valid subarray exists, output nothing.
inputFormat
The input is read from standard input (stdin) and consists of three lines:
- The first line contains a single integer \(n\) representing the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
- The third line contains a single integer \(T\), the target sum.
outputFormat
Output the longest contiguous subarray whose sum equals \(T\) as space-separated integers on a single line. If no valid subarray is found, output an empty line.
## sample5
1 -1 5 -2 3
3
1 -1 5 -2