I have an array `A`

of size `n`

. I want to reorder the elements of `A`

into another array `B`

in such a way that elements that lie far apart in `A`

are ordered first in `B`

. For example, if `n = 9`

, the two first elements of `B`

should be `A[0]`

and `A[8]`

, since these are the two elements furthest apart in `A`

(distance 8). `B[2]`

should be `A[4]`

, since that element is farthest from `A[0]`

and `A[8]`

(distance 4). Next we get `B[3] = A[2]`

and `B[4] = A[6]`

, since `A[2]`

and `A[6]`

are farthest from `A[0]`

, `A[4]`

, and `A[8]`

(minimum distance 2). Finally `A[1], A[3], A[5], A[7]`

in the last four positions of `B`

(minimum distance 1 from the already added elements).

What is a fast algorithm for doing this, and handling arrays for arbitrary size `n`

?