Skip to main content

Practice · 4 of 5

Shortest Subarray Sum

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** Non-negative integers only. Find the shortest contiguous subarray with sum at least S; print its length, or -1 if impossible. **Input:** Line 1: n S (1 <= n <= 200000, 1 <= S <= 10^18). Line 2: n integers (0 <= a[i] <= 10^9). **Output:** One integer — the shortest length or -1. **Example:** 6 8 / 2 3 1 2 4 3 -> 3 (2+4+... actually 4+3=7 no; 2+3+1+2=8 — wait: 1 2 4 sums 7; the minimal window is 2 3 1 2).

Back to lesson: Practice: Two Pointers Problem Set