Suppose I have an array of Size 4 and there are 5 elements
1) 0,1,2,3
2) 4,0,1,2 after 1st rotation
3) 3,4,0,1 after 2nd rotation
4) 2,3,4,0 after 3rd rotation
5) 1,2,3,4 after 4th rotation
6) 0,1,2,3 after 5th rotation
as we can see the number repeats itself after 5 iterations.
Is there an efficient way to search for kth element of the array ? after say N rotations??
Assume array indices are zero-based.
To compute the index of the Kth value of an M-element array after N rotations you should use the following expression
So given an array
arr, you should get the value like this (using C-like syntax):