#K81307. Longest Contiguous Subarray with Given Sum
Longest Contiguous Subarray with Given Sum
Longest Contiguous Subarray with Given Sum
You are given an array of integers and a target integer \(T\). Your task is to find the longest contiguous subarray whose elements sum up to \(T\). If there are multiple subarrays with the desired sum, output the one with the greatest length. If no such subarray exists, output an empty line.
Example:
Input: 5 1 -1 5 -2 3 3</p>Output: 1 -1 5 -2
Note: The input will contain multiple test cases. For each test case, the first line contains a single integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers. The third line contains the target integer \(T\). For each test case, output the longest contiguous subarray's elements separated by a single space. If no valid subarray exists, output an empty line.
inputFormat
The first line contains an integer \(T\), the number of test cases. For each test case the input format is as follows:
- An integer \(n\), the number of elements in the array.
- A line with \(n\) space-separated integers representing the array.
- An integer \(target\), the target sum.
All input is read from standard input.
outputFormat
For each test case, print a single line containing the longest contiguous subarray (its elements separated by a space) that sums to the given target. If no such subarray exists, print an empty line. All output should be sent to standard output.
## sample4
5
1 -1 5 -2 3
3
4
-2 -1 2 1
1
5
1 2 3 4 5
15
5
1 2 3 4 5
50
1 -1 5 -2
-1 2
1 2 3 4 5
</p>