Google onsite question
Anonymous User
6092

Given an array with N integers, starting index S and a value X. You are playing game in which you start from S and first move is always odd. If the move is odd you can jump to first index on the left which has value A[idx]+1. If the move is even you the same on right side. Whenever we make a jump update the previous position by X. Output the end position when you can not make any jump. If the game is going to be infinite return -1 or return the end index.

From example

A = [3,4,2,2,7]
X = 4
S=2

we are at index 2, A[2] = 2; move is odd 2+1=3 exists at index 0 Array will become this
3,4,6,2,7
Now we are at index 0, make a jump because 4 exists on right (even move)

7,4,6,2,7
Now at index 1 4+1 5 doesnt exist on the right of 5

Answer is 1 final index.

A = [2,1]
X = 2
S = 1

This is an infinite case.

Comments (22)