Smallest Subarray Sum Equals K - aloalgo

Smallest Subarray Sum Equals K

Hard

You are given an array of integers nums and an integer k, find the length of the shortest contiguous subarray whose sum is exactly k. If there is no such subarray, return -1.

Example 1

Inputs
nums = [1, 2, 3, 4, 5]
k = 9
Output
2
Explanation:

The subarray [4, 5] has a sum of 9 and is the shortest such subarray.

Example 2

Inputs
nums = [1, -1, 5, -2, 3]
k = 3
Output
1
Explanation:

The subarray [3] has a sum of 3 and is the shortest such subarray.

Example 3

Inputs
nums = [1, 2, 3]
k = 10
Output
-1
Explanation:

No subarray sums to 10.

Loading...
Inputs
nums = [1, 2, 3, 4, 5]
k = 9
Output
2

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!