Kth Smallest Subarray Sum - Problem

You're given an integer array nums of length n and an integer k. Your task is to find the kth smallest subarray sum among all possible subarrays.

A subarray is a contiguous sequence of elements within the array - for example, in array [1, 2, 3, 4], some subarrays are [1], [2, 3], [1, 2, 3, 4], etc. The subarray sum is simply the sum of all elements in that subarray.

Goal: Among all n(n+1)/2 possible subarray sums, return the kth smallest one (1-indexed).

Example: For nums = [2, 1, 3] and k = 4, the subarray sums are: [2] = 2, [1] = 1, [3] = 3, [2,1] = 3, [1,3] = 4, [2,1,3] = 6. Sorted: [1, 2, 3, 3, 4, 6]. The 4th smallest is 3.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2, 1, 3], k = 4
โ€บ Output: 3
๐Ÿ’ก Note: All subarrays: [2]=2, [2,1]=3, [2,1,3]=6, [1]=1, [1,3]=4, [3]=3. Sorted sums: [1,2,3,3,4,6]. The 4th smallest is 3.
example_2.py โ€” Single Element
$ Input: nums = [5], k = 1
โ€บ Output: 5
๐Ÿ’ก Note: Only one subarray [5] with sum 5, so the 1st smallest is 5.
example_3.py โ€” All Equal Elements
$ Input: nums = [3, 3, 3], k = 2
โ€บ Output: 3
๐Ÿ’ก Note: Subarrays: [3]=3, [3,3]=6, [3,3,3]=9, [3]=3, [3,3]=6, [3]=3. Sorted: [3,3,3,6,6,9]. The 2nd smallest is 3.

Constraints

  • 1 โ‰ค nums.length โ‰ค 2000
  • 1 โ‰ค nums[i] โ‰ค 5 ร— 104
  • 1 โ‰ค k โ‰ค nums.length ร— (nums.length + 1) / 2
  • All elements are positive integers

Visualization

Tap to expand
๐Ÿ“š The Bookshelf Reading ChallengeBooks: [2 pages, 1 page, 3 pages]213๐ŸŽฏ Goal: Find 4th shortest reading sessionReading sessions (consecutive books):๐Ÿ“– [Book1] = 2 pages๐Ÿ“š [Book1,Book2] = 3 pages๐Ÿ“š [Book1,Book2,Book3] = 6 pages๐Ÿ“– [Book2] = 1 page๐Ÿ“š [Book2,Book3] = 4 pages๐Ÿ“– [Book3] = 3 pages๐Ÿ” Smart Strategy: Binary Search + CountingInstead of listing all sessions, we ask:โ“ "How many sessions have โ‰ค 3 pages?"๐Ÿงฎ Use sliding window to count: 4 sessionsโœ… Since we want the 4th shortest, answer = 3!Binary Search ProcessRange: [1, 6] โ†’ try mid=3Count sessions โ‰ค 3: found 44 โ‰ฅ k=4, so answer โ‰ค 3Range: [1, 3] โ†’ try mid=2Count sessions โ‰ค 2: found 22 < k=4, so answer > 2Answer: 3 pages
Understanding the Visualization
1
Set the range
Minimum pages = shortest book, Maximum pages = all books combined
2
Make a guess
Try the middle value: 'How many sessions have โ‰ค this many pages?'
3
Count efficiently
Use a sliding window approach to count sessions without listing them all
4
Adjust and repeat
If count < k, guess higher; if count โ‰ฅ k, guess lower until exact
Key Takeaway
๐ŸŽฏ Key Insight: Binary search on the answer space + efficient counting eliminates the need to generate and sort all subarray sums, reducing complexity from O(nยฒ log n) to O(n log(sum)).
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
38.5K Views
Medium-High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen