Of course you could go backwards (4,3,2,1), but it would be much faster to go (4,5,1) because the numbers loop. How would I decide to keep going forward versus going backwards to reach a number?
Well, as long as they’re all ordered and you know the starting number, you can just see if the difference between the starting number and the selected number is greater than the largest number → the starting number and then adding that to the bottom number → selected number. This’ll look something like such:
local function fastIter(array, position)
if position <= #array/2 then
print("Distance from start is shorter.")
for index = position, 1, -1 do
print(array[index])
end
elseif position > #array/2 then
print("Distance to end is shorter.")
for index = position, #array do
print(array[index])
end
end
end
This should cover essentially what you’re asking for.