22. Sum of Subarray Ranges
β GFG solution to the Sum of Subarray Ranges problem: calculate sum of ranges across all subarrays using monotonic stack technique for optimal O(n) performance. π
π§© Problem Description
π Examples
Example 1
Input: arr[] = [1, 2, 3]
Output: 4
Explanation: The 6 subarrays of arr are:
[1], range = 1 - 1 = 0
[2], range = 2 - 2 = 0
[3], range = 3 - 3 = 0
[1, 2], range = 2 - 1 = 1
[2, 3], range = 3 - 2 = 1
[1, 2, 3], range = 3 - 1 = 2
Sum of all ranges = 0 + 0 + 0 + 1 + 1 + 2 = 4Example 2
π Constraints
β
My Approach
Monotonic Stack Contribution Method
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated