You are given an array values of positive integers.
You may modify the values; increasing or decreasing a value by 1 costs 1. Make all values equal (to any integer you choose) at the minimum possible total cost.
Return that minimum total cost.
Examples
Example 1
Input: values = [2,3,1,5,2]
Output: 5
Explanation: Make every value 2: costs 0 + 1 + 1 + 3 + 0 = 5. No other common target is cheaper.
Example 2
Input: values = [1,1000000000]
Output: 999999999
Explanation: Any common target between the two values costs their difference in total.
Constraints
1≤values.length≤2⋅105
1≤values[i]≤109
Examples
Example 1
Input
values = [2, 3, 1, 5, 2]
Output
5
Example 2
Input
values = [1, 1000000000]
Output
999999999
Loading editor...
Click "Run" to test with sample cases or "Submit" to run all tests.