#C9261. Shortest Subarray with Sum at Least K

    ID: 53335 Type: Default 1000ms 256MiB

Shortest Subarray with Sum at Least K

Shortest Subarray with Sum at Least K

Given an array of integers and an integer \( k \), your task is to determine the length of the shortest contiguous subarray whose sum is at least \( k \). If no such subarray exists, output \( -1 \).

The problem can be solved efficiently using a prefix sum array and a double-ended queue (deque) to keep track of potential subarray endpoints. The key observation is that if \( prefix[i] - prefix[j] \) is at least \( k \) (with \( i > j \)) then the subarray \( arr[j...i-1] \) meets the requirement. The goal is to minimize \( i - j \).

Input is read from standard input (stdin) and output is written to standard output (stdout).

inputFormat

The first line 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 \) space-separated integers representing the elements of the array.

outputFormat

Output a single integer representing the length of the shortest contiguous subarray with a sum of at least \( k \). If no such subarray exists, output \( -1 \).

## sample
6 15
1 2 3 4 5 10
2