My Solution:

Discussion on codechef:

Assume the first row is 72314 (no particular significance). Let's divide a rotation of the sequence 1234..n into 2 pieces, [start value to n] and [1 to end value]. When n=5, all rotations are shown below with | symbolizing the split between the pieces.

. [7 2 3 1 4] 1 2 3 4 5 8 4 6 5 9 2 3 4 5| 1 9 5 7 6| 5 3 4 5| 1 2 10 6 8| 2 6 4 5| 1 2 3 11 7| 4 3 7 5| 1 2 3 4 12| 3 5 4 8

Now you can observe observe that each value before the split in the *rth* rotation is just **(r+the value at that index in the 0***th* rotation). And each value after the split in any rotation is **(r-N+the value at that index in the 0***th* rotation). Let *maxr*(*i*,*j*) denote the maximum element from indices i to j in the *rth* rotation. Then it is follows that *maxr*(0, i) equals **r+***max*0(0, i) for **i<N-r**. This condit...