862. Shortest Subarray with Sum at Least K LeetCode Solution
In this guide, you will get 862. Shortest Subarray with Sum at Least K LeetCode Solution with the best time and space complexity. The solution to Shortest Subarray with Sum at Least K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Shortest Subarray with Sum at Least K solution in C++
- Shortest Subarray with Sum at Least K solution in Java
- Shortest Subarray with Sum at Least K solution in Python
- Additional Resources

Problem Statement of Shortest Subarray with Sum at Least K
Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1.
A subarray is a contiguous part of an array.
Example 1:
Input: nums = [1], k = 1
Output: 1
Example 2:
Input: nums = [1,2], k = 4
Output: -1
Example 3:
Input: nums = [2,-1,2], k = 3
Output: 3
Constraints:
1 <= nums.length <= 105
-105 <= nums[i] <= 105
1 <= k <= 109
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
862. Shortest Subarray with Sum at Least K LeetCode Solution in C++
class Solution {
public:
int shortestSubarray(vector<int>& nums, int k) {
const int n = nums.size();
int ans = n + 1;
deque<int> dq;
vector<long> prefix{0};
for (int i = 0; i < n; ++i)
prefix.push_back(prefix.back() + nums[i]);
for (int i = 0; i < n + 1; ++i) {
while (!dq.empty() && prefix[i] - prefix[dq.front()] >= k)
ans = min(ans, i - dq.front()), dq.pop_front();
while (!dq.empty() && prefix[i] <= prefix[dq.back()])
dq.pop_back();
dq.push_back(i);
}
return ans <= n ? ans : -1;
}
};
/* code provided by PROGIEZ */
862. Shortest Subarray with Sum at Least K LeetCode Solution in Java
class Solution {
public int shortestSubarray(int[] nums, int k) {
final int n = nums.length;
int ans = n + 1;
Deque<Integer> dq = new ArrayDeque<>();
long[] prefix = new long[n + 1];
for (int i = 0; i < n; ++i)
prefix[i + 1] = (long) nums[i] + prefix[i];
for (int i = 0; i < n + 1; ++i) {
while (!dq.isEmpty() && prefix[i] - prefix[dq.getFirst()] >= k)
ans = Math.min(ans, i - dq.pollFirst());
while (!dq.isEmpty() && prefix[i] <= prefix[dq.getLast()])
dq.pollLast();
dq.offerLast(i);
}
return ans <= n ? ans : -1;
}
}
// code provided by PROGIEZ
862. Shortest Subarray with Sum at Least K LeetCode Solution in Python
class Solution:
def shortestSubarray(self, nums: list[int], k: int) -> int:
n = len(nums)
ans = n + 1
dq = collections.deque()
prefix = list(itertools.accumulate(nums, initial=0))
for i in range(n + 1):
while dq and prefix[i] - prefix[dq[0]] >= k:
ans = min(ans, i - dq.popleft())
while dq and prefix[i] <= prefix[dq[-1]]:
dq.pop()
dq.append(i)
return ans if ans <= n else -1
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.