#K52492. Minimum Subarray Length
Minimum Subarray Length
Minimum Subarray Length
You are given an array of non-negative integers and a target integer T. Your task is to find the minimum length of a contiguous subarray such that the sum of its elements is at least T. More formally, given an array a of length n and a target T, you need to find the smallest integer L for which there exist indices 0 \le l \le r < n satisfying
If no such subarray exists, output -1
.
Note: It is guaranteed that all elements in the array are non-negative, which enables the use of a sliding window (two-pointer) technique to solve the problem efficiently.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains two space-separated integers:
n
(the number of elements in the array) andT
(the target sum). - The second line contains
n
space-separated non-negative integers representing the array elements.
outputFormat
Output a single integer to standard output (stdout) representing the minimum length of a contiguous subarray for which the sum is at least T
. If there is no such subarray, output -1
.
6 7
2 3 1 2 4 3
2