Example
Here is an example of understanding the problem of finding an element at a given index after several rotations.
Input:
array = {1, 5, 3, 4, 8},
rotations = {{0, 2}, {1, 3}, {0, 4}}, and
index = 2
Output:
Element at index two after rotations is: 4
Explanation:
After {0, 2}, the array will become {3, 1, 5, 4, 8}
After {1, 3}, the array will become {3, 4, 1, 5, 8}
After {0, 4}, the array will become {8, 3, 4, 1, 5}
Now, we have the element at index 2 is 4.
Also see, Euclid GCD Algorithm
Algorithm
It's time to learn the Methods to find the element. We will see the algorithms as solutions to our problem. Why wait? Letâ€™s together learn something new.
Source
Method 1
The very first method is the Bruteforce method. This algorithm consists of the following steps:
 Properly rotate the array for all specified ranges.
 Then return the element in the updated array at the given index.
Method 2
We can analyse the supplied set of ranges without rotating the array instead of executing all of the rotations and locating the member at the given index. It consists of the following processes:
 Iterate through all the sets of ranges in reverse order(from last to first).
 If the index is larger than left and lower than right;

If yes, see if the index is equal to the left; move the index to the right.
Otherwise, decrement the index.
 At the index position, print the array element.
Working
Till now, we have learned examples and algorithms. But how are these algorithms working? What is happening precisely in these algorithms?
Source
To answer all these questions, letâ€™s understand the working of indexbased(Method 2) algorithms in detail.
 Given Array: arr = {1, 5, 3, 4, 8}
 Given Rotations: { (0, 2), (1, 3), (0,4) }
 Index = 2

Range (0, 4) and index = 2.
Since, Left range < = Index < = Right Range and Index ! = 0.
Decrement Index.
Now Index = 1.

Range (1, 3) and index = 1.
Since, Left range = Index.
Set Index to Right Range Value.
Now Index = 3.

Range (0, 2) and index = 3.
Since the Index is not inbound.
Do nothing.
So the new index is three, and our output is 4.
Now, letâ€™s verify the result with the bruteforce method:

First Rotation: (0, 2)
Updated array: arr={ 3, 1, 5, 4, 8}

Second Rotation: (1, 3)
Updated array: arr={ 3, 4, 1, 5, 8}

Third Rotation: (0, 4)
Updated array: arr={ 8, 3, 4, 1, 5}
So the element at index 2 is 4, which is the same as the previous step. This is the reason that we process the ranges in reverse order.
Implementation
Here is the implementation of an effective solution to the problem in different languages.
Approach 1 (C++)
The given code is the implementation of our algorithm in C++ language.
Code
#include <bits/stdc++.h>
using namespace std;
// Function to perform rotations and find index element
int Element(int arr[], int ranges[][2],
int rotations, int index)
{
for (int i = rotations  1; i >= 0; i) {
int left = ranges[i][0];
int right = ranges[i][1];
// checking left and right condition
if (left <= index && right >= index) {
if (index == left)
index = right;
else
index;
}
}
// Returning the new element
return arr[index];
}
// Main function
int main()
{
int arr[100], n, element;
cout << "Enter the Number of elements: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter the element " << i + 1 << ": ";
cin >> arr[i];
}
int rotations;
cout << "Enter the number of rotations: ";
cin >> rotations;
int ranges[rotations][2];
for (int i = 0; i < rotations; i++) {
cout << "For Rotation " << i + 1 << endl;
cout << "Enter left Range: ";
cin >> ranges[i][0];
cout << "Enter right Range: ";
cin >> ranges[i][1];
}
int index;
cout << "Enter the index: ";
cin >> index;
cout << "Element at the index " << index << " after all the rotations is: " << Element(arr, ranges, rotations, index);
return 0;
}
Output
Approach 2 (Python 3)
The given code is the implementation of our algorithm using the Python language.
Code
# Function to find the element
def Element(arr, ranges, rotations, index) :
for i in range(rotations  1, 1, 1 ) :
# Ranges
left = ranges[i][0]
right = ranges[i][1]
if (left <= index and right >= index) :
if (index == left) :
index = right
else :
index = index  1
# Returning the new element
return arr[index]
# Driver Code
arr = [ 1, 5, 3, 4, 8 ]
# No. of rotations
rotations = 3
# Ranges according to
# 0based indexing
ranges = [ [ 0, 2 ], [ 1, 3 ], [ 0, 4 ]]
index = 2
print("The element at given index:", Element(arr, ranges, rotations, index))
Output
Approach 3 (C#)
The given code is the implementation of our algorithm using the C# language.
Code
using System;
class Array
{
// Function to find the element
static int Element(int []arr, int [,]ranges, int rotations, int index)
{
for (int i = rotations  1; i >= 0; i)
{
// Range
int left = ranges[i, 0];
int right = ranges[i, 1];
// Rotation will not
// have any effect
if (left <= index &&
right >= index)
{
if (index == left)
index = right;
else
index;
}
}
// Returning new element
return arr[index];
}
// Driver Code
public static void Main ()
{
int []arr = { 1, 5, 3, 4, 8 };
// The No. of rotations
int rotations = 3;
// Ranges
int [,]ranges = { { 0, 2 },
{ 1, 3 }, { 0, 4 } };
int index = 2;
Console.Write("The element at given index is:" +Element(arr, ranges, rotations, index));
}
}
Output
Check out this problem  First And Last Occurrences Of X
Frequently Asked Questions
Is it possible to modify the size of an array during execution?
No, we won't be able to adjust the array size. There are, however, similar data types that allow for size changes.
What do you mean by rotation of an array?
Array rotation implies moving the array elements to the left or right by a specified amount. An array can be rotated to the provided number of positions to the left (clockwise) or right (anticlockwise).
What do you mean by circular right rotation?
When all items of an array are moved one position to the right, the array is said to be right rotated.
What is the time complexity of Circular right rotation?
The time complexity of Circular right rotation is O(n), where n is the number of elements in the given array.
Conclusion
This blog discussed finding an element at a given index after several rotations.
We have discussed,
 Example of finding the element.
 Algorithm to find the element using bruteforce method and then using index method.
 Working of the Algorithm.
 Implementation of the Algorithm.
This blog is related to array rotation, so it is advised to check these articles on Rotation Count and Circular right rotation. You can learn the Block Swap algorithm for Array Rotation.
To learn more about array rotation, visit the Array Rotation Section at Coding Ninjas Studio.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!
We wish you Good Luck! Keep coding and keep reading Ninja!!