Sum of Subarray Ranges - Problem

You are given an integer array nums. The range of a subarray is defined as the difference between the largest and smallest element in that subarray.

Your task is to return the sum of all subarray ranges of nums.

A subarray is a contiguous non-empty sequence of elements within an array. For example, if nums = [1, 2, 3], the subarrays are: [1], [2], [3], [1, 2], [2, 3], and [1, 2, 3].

Example: For nums = [1, 2, 3]:
- Subarray [1]: range = 1 - 1 = 0
- Subarray [2]: range = 2 - 2 = 0
- Subarray [3]: range = 3 - 3 = 0
- Subarray [1, 2]: range = 2 - 1 = 1
- Subarray [2, 3]: range = 3 - 2 = 1
- Subarray [1, 2, 3]: range = 3 - 1 = 2
Total sum: 0 + 0 + 0 + 1 + 1 + 2 = 4

Input & Output

example_1.py — Simple Array
$ Input: [1, 2, 3]
› Output: 4
šŸ’” Note: Subarrays: [1] (range=0), [2] (range=0), [3] (range=0), [1,2] (range=1), [2,3] (range=1), [1,2,3] (range=2). Total: 0+0+0+1+1+2=4
example_2.py — Single Element
$ Input: [1]
› Output: 0
šŸ’” Note: Only one subarray [1] with range = 1-1 = 0
example_3.py — Larger Array
$ Input: [1, 3, 3]
› Output: 4
šŸ’” Note: Subarrays: [1] (range=0), [3] (range=0), [3] (range=0), [1,3] (range=2), [3,3] (range=0), [1,3,3] (range=2). Total: 0+0+0+2+0+2=4

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -109 ≤ nums[i] ≤ 109
  • Note: The array can contain duplicate elements

Visualization

Tap to expand
šŸŒ”ļø Temperature Range Analysis1°C2°C3°CHour 1Hour 2Hour 3šŸ”„ Maximum Contributions (Hottest in time windows):• 1°C is max in: [1] → 1 window• 2°C is max in: [2], [1,2] → 2 windows• 3°C is max in: [3], [2,3], [1,2,3] → 3 windowsSum of maximums: 1Ɨ1 + 2Ɨ2 + 3Ɨ3 = 14ā„ļø Minimum Contributions (Coldest in time windows):• 1°C is min in: [1], [1,2], [1,2,3] → 3 windows• 2°C is min in: [2], [2,3] → 2 windows• 3°C is min in: [3] → 1 windowSum of minimums: 1Ɨ3 + 2Ɨ2 + 3Ɨ1 = 10šŸ—ļø Monotonic Stack MagicDecreasing Stack(finds maximums)Increasing Stack(finds minimums)Stack operations count how manysubarrays each element dominatesTime Complexity: O(n)šŸŽÆ Final Temperature Range SumSum of Maximums - Sum of Minimums = 14 - 10 = 4
Understanding the Visualization
1
Identify the insight
Range = Max - Min, so sum of ranges = sum of all maximums - sum of all minimums
2
Use monotonic stacks
Track when each element is maximum/minimum using decreasing/increasing stacks
3
Count contributions
For each element, count how many subarrays it dominates as max/min
4
Calculate final result
Multiply each element by its contribution count and sum the differences
Key Takeaway
šŸŽÆ Key Insight: Instead of generating O(n²) subarrays, we use monotonic stacks to mathematically count each element's contribution as maximum/minimum, reducing time complexity from O(n³) to O(n).
Asked in
Google 28 Amazon 22 Meta 18 Microsoft 15
68.2K Views
Medium Frequency
~25 min Avg. Time
1.8K 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