Problems > Shortest Reaching Subarray (Negatives)

Shortest Reaching Subarray (Negatives)

Difficulty: 2200 5000 ms 128 MB

prefix-sums deque

Values may be negative. Find the length of the shortest contiguous subarray with sum at least K, or −1.

Input: the first line has N and K; the second line has N integers.

Output: the minimum length, or −1.

Constraints:

  • 1 ≤ N ≤ 105
  • |ai| ≤ 104
  • |K| ≤ 109

Sample Tests

Input 1
5 3
2 -1 2 -3 4
Output 1
1
Input 2
3 100
1 2 3
Output 2
-1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.