FunctionRacerProblemsRace

Spread of Every Selection

HardArraysPython, JavaScript, TypeScript, Java, C++

A selection picks one or more positions of nums. Two selections are different when they pick different sets of positions, even if the values are the same. The spread of a selection is its largest picked value minus its smallest, so a selection of one position has spread 0.

Return the sum of the spreads of all selections.

Example 1

nums [4,1,2]
returns 9

The pairs have spreads 3 (4 and 1), 2 (4 and 2) and 1 (1 and 2), and all three together have spread 3. Single items add 0.

Example 2

nums [5,5,5]
returns 0

Every selection has spread 0, because all the values are equal.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems