#K81307. Longest Contiguous Subarray with Given Sum

    ID: 35724 Type: Default 1000ms 256MiB

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

Output: 1 -1 5 -2

</p>

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:

  1. An integer \(n\), the number of elements in the array.
  2. A line with \(n\) space-separated integers representing the array.
  3. 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.

## sample
4
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>