Tap here to switch tabs
Problem
Submissions

Problem: Sum of Subarray Ranges

Sum of Subarray Ranges asks you to examine every nonempty contiguous subarray, calculate its maximum minus minimum, and return the total of those ranges. Values may be negative.

Statement

Given an integer array nums, consider every non empty contiguous subarray of nums. For each such subarray, define its range as the difference between its maximum element and its minimum element.

Return the sum of these ranges over all non empty contiguous subarrays of nums.

Note: A follow up asks whether you can solve this in O(n)O(n) time.

Constraints:

  • 1≤1 \leq nums.length ≤1000\leq 1000

  • −109≤-10^9 \leq nums[i] ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Sum of Subarray Ranges

Sum of Subarray Ranges asks you to examine every nonempty contiguous subarray, calculate its maximum minus minimum, and return the total of those ranges. Values may be negative.

Statement

Given an integer array nums, consider every non empty contiguous subarray of nums. For each such subarray, define its range as the difference between its maximum element and its minimum element.

Return the sum of these ranges over all non empty contiguous subarrays of nums.

Note: A follow up asks whether you can solve this in O(n)O(n) time.

Constraints:

  • 1≤1 \leq nums.length ≤1000\leq 1000

  • −109≤-10^9 \leq nums[i] ≤109\leq 10^9