Code RoomSum of subarray ranges
HardPrep Room Coding #1502

Sum of subarray ranges

CodingAlgorithms & data structuresSenior–Staff~35 min

Given an integer array, the range of a contiguous subarray is its maximum element minus its minimum element. Return the sum of the ranges over all contiguous subarrays. The array has at most 1000 elements; values may be negative. The answer fits in a 64-bit signed integer.

Implement
sum_subarray_ranges(arr: list[int]) → int
Examples
in[[1,2,3]]out4
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[[1,2,3]]4not run yetsample