Distribute Values by Distance from Current Averages
Problem statement
Initialize first with values[0] and second with values[1]. For every later value, compare its absolute distance from each array's current arithmetic mean and append it to the array with the smaller distance. Break an exact tie in favor of first.
Return first followed by second.
Function
distributeByAverage(values: int[]) → int[]Examples
Example 1
values = [1,10,2,9,5]return = [1,2,5,10,9]Later values are compared against evolving means.
Example 2
values = [0,10,5]return = [0,5,10]Equal average distances choose the first array.
Constraints
2 <= values.length <= 100000-100000 <= values[i] <= 100000