Range Sum of Sorted Subarray Sums - Problem
Range Sum of Sorted Subarray Sums

Imagine you have an array of positive integers, and you want to explore all possible continuous subarrays within it. Your task is to compute the sum of every possible subarray, sort these sums in ascending order, and then find the sum of elements within a specific range.

Here's the process:
1. Generate all subarray sums: For an array of size n, there are exactly n × (n + 1) / 2 continuous subarrays
2. Sort the sums: Arrange all subarray sums in non-decreasing order
3. Range sum: Calculate the sum from index left to right (1-indexed) in the sorted array

Goal: Return the range sum modulo 109 + 7 since the answer can be very large.

Example: For array [1,2,3,4], subarrays are [1],[2],[3],[4],[1,2],[2,3],[3,4],[1,2,3],[2,3,4],[1,2,3,4] with sums [1,2,3,4,3,5,7,6,9,10]. After sorting: [1,2,3,3,4,5,6,7,9,10].

Input & Output

example_1.py — Basic Case
$ Input: nums = [1,2,3,4], n = 4, left = 1, right = 5
Output: 13
💡 Note: All subarray sums are [1,2,3,4,3,5,7,6,9,10]. After sorting: [1,2,3,3,4,5,6,7,9,10]. Sum of 1st to 5th elements: 1+2+3+3+4 = 13.
example_2.py — Full Range
$ Input: nums = [1,2,3,4], n = 4, left = 3, right = 4
Output: 6
💡 Note: From sorted subarray sums [1,2,3,3,4,5,6,7,9,10], elements at positions 3-4 are [3,3]. Sum = 3+3 = 6.
example_3.py — Single Element Array
$ Input: nums = [1], n = 1, left = 1, right = 1
Output: 1
💡 Note: Only one subarray [1] with sum 1. The range [1,1] contains just this element, so answer is 1.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 1000
  • 1 ≤ nums[i] ≤ 100
  • 1 ≤ left ≤ right ≤ n × (n + 1) / 2
  • Answer must be returned modulo 109 + 7

Visualization

Tap to expand
Range Sum of Sorted Subarray Sums VisualizationStep 1: Original Array [1, 2, 3, 4]1234Step 2: All Subarray SumsLength 1: [1]=1, [2]=2, [3]=3, [4]=4Length 2: [1,2]=3, [2,3]=5, [3,4]=7Length 3: [1,2,3]=6, [2,3,4]=9Length 4: [1,2,3,4]=10All sums: [1, 2, 3, 4, 3, 5, 7, 6, 9, 10]Step 3: Sorted Sums12334567910Positions: 1 2 3 4 5 6 7 8 9 10Step 4: Range Sum [left=3, right=6]Sum of positions 3-6: 3 + 3 + 4 + 5 = 15🎯 Key insight: Each element contributes to multiple subarrays in a predictable pattern⚡ Optimization: Use binary search to avoid storing all O(n²) sums explicitly
Understanding the Visualization
1
Generate All Subarrays
Create every possible continuous subarray from the original array
2
Calculate Sums
Compute the sum for each subarray we generated
3
Sort by Value
Arrange all subarray sums in ascending order
4
Extract Range
Sum the elements from position left to right in sorted array
Key Takeaway
🎯 Key Insight: While the brute force approach directly implements the problem description, binary search optimization can significantly reduce space complexity from O(n²) to O(1) by cleverly counting subarray sums without storing them all.
Asked in
Google 15 Amazon 8 Microsoft 6 Meta 4
28.5K Views
Medium Frequency
~15 min Avg. Time
891 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