Let's play a one-player game called jump the array. To play, you only need an array of integers, say a
. You start at some position i
, and on each turn, you jump to a new position. On turn n
,
- if
n
is even, you jump to the absolute positiona[i] mod length(a)
, - if
n
is odd, you jump to the relative position(i + a[i]) mod length(a)
.
The array indexing starts at zero. You can count the first jump as turn 0
or turn 1
, which give a different game. Since the state space of the game is finite (your move is determined by your position and the parity of the turn number), you will of course eventually enter a loop of even length. Denote by loop(a, i, b)
the length of this loop, when the first jump is counted as turn b
.
Input
A nonempty array a
of integers to play the game with.
Output
The maximum number p
such that, when starting on some position i
and counting the first turn as either 0
or 1
, you eventually enter a loop of length 2 * p
. In other words, your output is the number
max { loop(a, i, b)/2 : i in [0 .. length(a)-1], b in [0,1] }
Rules
You can give a function or a full program. The smallest byte count wins, and standard loopholes are disallowed.
Test cases
[0] -> 1
[-213] -> 1
[1,3,12,-1,7] -> 1
[2,3,5,7,9,11,13,17,19] -> 2
[-2,3,-5,7,-9,11,-13,17,-19,23,-27] -> 3
[0,2,5,4,-9,0,-1,1,-1,1,-6] -> 4
mod
is defined as always positive (-1 mod 5 == 4
) unlike in C. Is that the case? \$\endgroup\$mod
, which always gives nonnegative results. \$\endgroup\$