Practice › Sliding Window
Shortest Subarray with Sum at Least K
Hard · Sliding Window · Asked at Google, Amazon, Goldman Sachs
The array may contain negative numbers. Print the length of the shortest non-empty contiguous subarray whose sum is at least k, or -1 if there is none.
Input
First line: n k. Second line: n integers.
Output
The shortest length, or -1.
Constraints
- 1 ≤ n ≤ 10^5
- −10^5 ≤ a[i] ≤ 10^5
- 1 ≤ k ≤ 10^9
With negatives, a plain sliding window fails. Use prefix sums P and a deque of indices with increasing P. For each j: while P[j] − P[front] ≥ k, record j − front and pop the front; then pop from the back every index with P ≥ P[j] before pushing j.
Example 1
Input
1 1 1
Output
1
Example 2
Input
2 4 1 2
Output
-1
Example 3
Input
3 3 2 -1 2
Output
3
Hints
- Negative numbers break the usual two-pointer window: shrinking can increase the sum.
- Work with prefix sums P: a subarray (i, j] has sum P[j] − P[i].
- Keep candidate starts i in a deque with increasing P[i].
Approach
For each j from 0 to n: while P[j] − P[front] ≥ k, record j − front and pop the front — no later j can give a shorter subarray from that start. Then pop from the back every index whose P is ≥ P[j]: a later start with a smaller or equal prefix sum is always at least as good. Push j. Each index enters and leaves the deque once.
Complexity: O(n) time · O(n) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long k;
cin >> n >> k;
vector<long long> P(n + 1, 0);
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
P[i + 1] = P[i] + x;
}
deque<int> dq;
int best = INT_MAX;
for (int j = 0; j <= n; j++) {
while (!dq.empty() && P[j] - P[dq.front()] >= k) {
best = min(best, j - dq.front());
dq.pop_front();
}
while (!dq.empty() && P[dq.back()] >= P[j]) dq.pop_back();
dq.push_back(j);
}
cout << (best == INT_MAX ? -1 : best) << "\n";
return 0;
}Python solution
import sys
from collections import deque
data = list(map(int, sys.stdin.read().split()))
n, k = data[0], data[1]
P = [0] * (n + 1)
for i in range(n):
P[i + 1] = P[i] + data[2 + i]
dq = deque()
best = n + 1
for j in range(n + 1):
while dq and P[j] - P[dq[0]] >= k:
best = min(best, j - dq.popleft())
while dq and P[dq[-1]] >= P[j]:
dq.pop()
dq.append(j)
print(best if best <= n else -1)Java solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
in.nextToken();
long k = (long) in.nval;
long[] P = new long[n + 1];
for (int i = 0; i < n; i++) {
in.nextToken();
P[i + 1] = P[i] + (long) in.nval;
}
int[] dq = new int[n + 1];
int head = 0, tail = 0, best = Integer.MAX_VALUE;
for (int j = 0; j <= n; j++) {
while (head < tail && P[j] - P[dq[head]] >= k) {
best = Math.min(best, j - dq[head]);
head++;
}
while (head < tail && P[dq[tail - 1]] >= P[j]) tail--;
dq[tail++] = j;
}
System.out.println(best == Integer.MAX_VALUE ? -1 : best);
}
}Solve Shortest Subarray with Sum at Least K online · All practice problems