We are given an array a[0..n-1] and we perform right circular rotation of the array by k times. After 1 rotation, we obtain the array {a[n – 1], a[0], …, a[n – 2]}. Now we have q queries in the rotated array, asking for the element value at different indexes.

For rotation problems, we need to keep in mind that if the number of rotations reach the length of the array, we get the original unrotated array back. So the number of rotations that are effective (i.e., change the array) is actually k % n, where k is the number of rotations and n is the array length. So we can write the following code.

Leave a Reply

Your email address will not be published. Required fields are marked *