javascript - Minimum distance between two elements in a circular array -


given circular array, efficient way determine minimum distance between 2 elements?

for example moving here

[0,0,0,0,0,1] 

to here

[1,0,0,0,0,0] 

is more convenient outer bounds, whilst moving here

[0,0,0,0,0,1] 

to here

[0,0,0,1,0,0] 

is more convenient internally.

my initial idea this:

sourcelocation = rotatorelements["pos"].indexof(1); targetlocation = rotatorelements["pos"].rotate(delta).indexof(1);  var d = math.abs(targetlocation - sourcelocation); var o = math.abs(rotatorelements["pos"].length - d);  if ((d - o == 0)) direction = 1; else {     if (d < o) direction = -1;     else direction = 1; } 

note: rotate rotates circular array defined number of positions.

edit: think answered question in title, want direction not distance. then:

function getdirection(from, to, array) {     if (from === to) {         return 0;     }     var internal = (math.max(from, to) - math.min(from, to) < array.length/2) ? true : false;     if (internal && <        ||        !internal && >     ) {        return 1;     } else {        return -1;     } } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -