FastPrepDistribute Values by Distance from Current Averages

Distribute Values by Distance from Current Averages

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

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

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int[] distributeByAverage(int[] values) {
    // Write your code here.
}
values[1,10,2,9,5]
expected[1,2,5,10,9]
Checking account…