#K51752. Shortest Contiguous Subarray with Sum at Least T
Shortest Contiguous Subarray with Sum at Least T
Shortest Contiguous Subarray with Sum at Least T
You are given an array of positive integers and a target integer \(T\). Your task is to determine the length of the shortest contiguous subarray whose sum is at least \(T\). If there is no such subarray, output 0.
Problem Explanation:
The input starts with two integers: \(n\) (the number of elements in the array) and \(T\) (the target sum). The next line contains \(n\) space-separated positive integers representing the array.
For example, given the array [5, 1, 3, 5, 10, 7] with \(T=15\), the shortest contiguous subarray that meets or exceeds \(T\) is [5, 10] having length 2.
The goal is to output the length of such a subarray if it exists, otherwise output 0.
inputFormat
The input is read from stdin and consists of two lines:
- 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.
- The second line contains \(n\) space-separated positive integers representing the array.
outputFormat
The output should be written to stdout and is a single integer: the length of the shortest contiguous subarray whose sum is at least \(T\). If no valid subarray exists, output 0.
## sample5 15
5 1 3 5 10 7
2