19
1
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
@kukac67 Yeah, it's the latter option, as Martin said. – Zgarb – 2015-01-02T18:25:06.610
I assume that
mod
is defined as always positive (-1 mod 5 == 4
) unlike in C. Is that the case? – nutki – 2015-01-02T20:53:38.453@nutki Yes, I use a Haskell-style
mod
, which always gives nonnegative results. – Zgarb – 2015-01-02T20:55:33.383If zero-indexing turns gives a different result from one-indexing, should we output either result, or whichever one is less? – KSFT – 2015-01-02T22:46:14.470
@MartinBüttner No, I was asking about indexing the turns, not the arrays. – KSFT – 2015-01-02T23:21:31.013
@KSFT oh. You should output the maximum over both using 0-based and 1-based counting (that's the
b
in the definition). (I know because I asked this earlier.) – Martin Ender – 2015-01-02T23:51:13.493