#K82752. Longest Subarray with Sum K
Longest Subarray with Sum K
Longest Subarray with Sum K
You are given an array of integers and an integer \(k\). Your task is to find the longest contiguous subarray that sums to \(k\). If there are multiple subarrays with the same maximum length, you must output the one that occurs first. If no subarray exists that sums to \(k\), output an empty result.
Note: The subarray should be printed as the list of numbers separated by a single space on one line. If no valid subarray is found, print an empty line.
Example:
Input: 5 12 1 2 3 7 5</p>Output: 2 3 7
In the above example, the subarray [2, 3, 7] is the longest one that adds up to 12.
inputFormat
The first line of input contains two integers: \(n\) and \(k\), where \(n\) is the number of elements in the array, and \(k\) is the target sum.
The second line contains \(n\) integers separated by spaces, representing the elements of the array.
Example:
5 12 1 2 3 7 5
outputFormat
Output a single line containing the longest contiguous subarray whose sum is equal to \(k\). The elements should be printed in order, separated by a single space. If there is no subarray with the required sum, print an empty line.
## sample5 12
1 2 3 7 5
2 3 7