As reported: Amazon online assessment (July 2025), coding question 2, "Minimize Total Variation" —
long minimizeVariation(vector<int> productSize); a sort plus sliding window approach failed the hidden tests.
You are given productSize, an array of n integers. For an arrangement a_1, a_2, …, a_n of these numbers, its total variation is
Σ for i = 1..n of ( max(a_1..a_i) − min(a_1..a_i) )
Return the minimum total variation over all arrangements.
Example 1
productSize = [6,1,4,2]
Output: 9 [1,2,4,6]: 0 + 1 + 3 + 5
Example 2
productSize = [10,1,3,8,2,9]
Output: 27 [1,2,3,8,9,10]: 0 + 1 + 2 + 7 + 8 + 9
Constraints: 1 ≤ n ≤ 2000; 1 ≤ productSize[i] ≤ 10^4.
Code runner format: one line: productSize as a JSON array. Implement minimizeVariation(productSize); the harness prints the result.