#C12425. Longest Contiguous Subarray with Given Sum

    ID: 41851 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 sum \(T\). Your task is to find the longest contiguous subarray whose elements sum up to \(T\). If there are multiple subarrays with the same maximum length, return the one that appears first. If there is no such subarray, output an empty result.

Note: The subarray must be contiguous.

Example:

Input: 6 8
       3 4 1 2 5 1
Output: 3 4 1

The mathematical formulation is: Find indices \(l\) and \(r\) (with \(0 \leq l \leq r < n\)) such that \[ \sum_{i=l}^{r} a_i = T \] and \(r-l+1\) is maximized. In case of tie, choose the smallest \(l\).

inputFormat

The first line contains two integers \(n\) and \(T\), where \(n\) is the number of elements in the array and \(T\) is the target sum.

If \(n > 0\), the second line contains \(n\) space-separated integers representing the array elements. If \(n = 0\), the second line will be empty.

outputFormat

Output the elements of the longest contiguous subarray that sums to \(T\) as space-separated integers on a single line. If the subarray is empty, output an empty line.

## sample
1 5
5
5

</p>