Loading...
You are given a list of integers nums. Split its elements into three groups, numbered group 1, group 2, and group 3. Every element goes into exactly one group, and no group may be empty.
After a split, pick one element from each group: x from group 1, y from group 2, and z from group 3. The cost of that pick is abs(x - y) + abs(y - z). The spread of the split is the smallest cost over every possible pick.
Return the largest spread you can get over every possible split of nums.
Note that the group numbers matter. y always comes from group 2, so swapping the contents of two groups gives a different split, which can have a different spread.
Note that equal values may go into different groups.
Note that the answer can exceed the 32-bit integer range.
Input: nums = [3,1,2]
Output: 3
Explanation: Each group gets exactly one element, so each split has only one pick.
Group 1 = [3], group 2 = [1], group 3 = [2] costs abs(3 - 1) + abs(1 - 2) = 3.
Group 1 = [3], group 2 = [2], group 3 = [1] costs abs(3 - 2) + abs(2 - 1) = 2.
These two splits use the same values in a different group order, yet their spreads differ.
Of all 6 splits, the largest spread is 3.
Input: nums = [1,5,9,10]
Output: 13
Explanation: Split into group 1 = [10], group 2 = [1], group 3 = [5,9].
There are two picks: z = 5 costs abs(10 - 1) + abs(1 - 5) = 13, and z = 9 costs
abs(10 - 1) + abs(1 - 9) = 17. The spread of this split is the smaller cost, 13.
No other split has a larger spread.
Input: nums = [5,5,5]
Output: 0
Explanation: Every pick is x = y = z = 5, so every cost is 0.
nums.length ≤105nums[i] ≤109