I have R
objects, where R >= 3
.
I have a N-sized array, where N > R
.
I would like to determine in which indexes of the array I must put the R
objects so the distance between any two neighbors will be equal, or the most similar possible.
Eg:
for N = 4
, R = 3
{1, 2, 3, 4}
{o, o, , o}
or
{1, 2, 3, 4}
{o, , o, o}
for N = 17
, R = 10
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}
{o, o, o, , o, , o, , o, , o, , o, , o, , o}
or
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}
{o, o, , o, , o, , o, , o, , o, , o, , o, o}
Of course I need the objects to be the most distant possible form each other, so this is unacceptable:
{1, 2, 3, 4, 5, 6, 7}
{o, o, o, o, , , }
{1, 2, 3, 4, 5, 6, 7, 8}
{o, , o, , o, , o, }
Basically the first and last position of the array will always be occupied.
Best Answer
Put the 0th element at index 0, the R-1th element at index N-1.
Calculate
d = floor(N / R)
.This function describes the target indexes for the 1st to the R-2th element:
i(x) = 2 * d * x
;Examles: