#K51367. Shortest Subarray with Sum at Least Target
Shortest Subarray with Sum at Least Target
Shortest Subarray with Sum at Least Target
You are given an array of non-negative integers and a target value \(T\). Your task is to find the shortest contiguous subarray such that the sum of its elements is at least \(T\). If such a subarray exists, output its elements in order separated by a space. Otherwise, output -1.
Note: If there are multiple subarrays with the same minimum length meeting the requirement, any one of them is acceptable.
Example: For the array [2, 3, 1, 2, 4, 3] and target \(T = 7\), the shortest subarray is [4, 3] since 4 + 3 = 7.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains two space-separated integers \(n\) and \(T\), where \(n\) is the number of elements in the array and \(T\) is the target sum.
- The second line contains \(n\) space-separated integers representing the array.
It is guaranteed that \(n \ge 0\), and each number is non-negative.
outputFormat
Output the shortest contiguous subarray with a sum that is at least \(T\) via standard output (stdout). The elements should be printed in order, separated by a space. If no such subarray exists, output -1.
## sample6 7
2 3 1 2 4 3
4 3