#K2316. Shortest Subarray with Sum at Least K
Shortest Subarray with Sum at Least K
Shortest Subarray with Sum at Least K
Given an array of integers arr
of length n and an integer target K, your task is to determine the length of the shortest contiguous subarray such that the sum of its elements is at least K. Formally, find the minimum length L such that there exists indices i and j (with 0 ≤ i ≤ j < n) satisfying
$$\sum_{k=i}^{j} arr[k] \ge K$$
If no such subarray exists, output -1
.
This problem requires efficient handling as the size of the array can be large, and naive solutions may not complete within the required time limits.
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains two space-separated integers: n (the number of elements in the array) and K (the target sum).
- The second line contains n space-separated integers representing the array
arr
.
Constraints: 1 ≤ n ≤ 105 and the elements of the array are integers.
outputFormat
Output a single integer to stdout: the length of the shortest contiguous subarray whose sum is at least K. If no such subarray exists, output -1
.
5 11
1 2 3 4 5
3