Open In App

C++ Program to Find element at given index after a number of rotations

Improve
Improve
Like Article
Like
Save
Share
Report

An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.
Examples : 
 

Input : arr[] : {1, 2, 3, 4, 5}
        ranges[] = { {0, 2}, {0, 3} }
        index : 1
Output : 3
Explanation : After first given rotation {0, 2}
                arr[] = {3, 1, 2, 4, 5}
              After second rotation {0, 3} 
                arr[] = {4, 3, 1, 2, 5}
After all rotations we have element 3 at given
index 1. 

Method: Brute-force The brute force approach is to actually rotate the array for all given ranges, finally return the element in at given index in the modified array.

Approach:

 We first iterate over the given ranges of rotations, and perform a right circular rotation on each range by moving the last element to the first position and shifting all the other elements to the right. We can perform this rotation in-place by using a temporary variable to store the last element, and shifting the elements in the range one by one to the right.

After performing all the rotations, we can return the element at the given index in the rotated array. Since we are performing the rotations in-place, the array is modified as we go.

The function then returns the element at index index in the rotated array.

Implementation:

C++




#include <bits/stdc++.h>
using namespace std;
 
void getElement_Index(int arr[], pair<int, int> ranges[], int n, int m, int index) {
    for (int i = 0; i < m; i++) {
        int l = ranges[i].first;
        int r = ranges[i].second;
        int temp = arr[r];
        for (int j = r; j > l; j--) {
            arr[j] = arr[j-1];
        }
        arr[l] = temp;
    }
    cout<<arr[index]<<endl;
}
 
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    pair<int, int> ranges[] = {{0, 2}, {0, 3}};
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = sizeof(ranges) / sizeof(ranges[0]);
    int index = 1;
    getElement_Index(arr, ranges, n, m, index);
    return 0;
}


Output

3

Time Complexity: O(n*m), where n represents the size of the array and m represents the size of ranges array .

Auxiliary Space: O(1), no extra space is required, so it is a constant.

Method : Efficient We can do offline processing after saving all ranges. 
Suppose, our rotate ranges are : [0..2] and [0..3] 
We run through these ranges from reverse.
After range [0..3], index 0 will have the element which was on index 3. 
So, we can change 0 to 3, i.e. if index = left, index will be changed to right. 
After range [0..2], index 3 will remain unaffected.
So, we can make 3 cases : 
If index = left, index will be changed to right. 
If index is not bounds by the range, no effect of rotation. 
If index is in bounds, index will have the element at index-1.
Below is the implementation : 
 

For better explanation:-

10 20 30 40 50

Index: 1

Rotations: {0,2} {1,4} {0,3}

Answer: Index 1 will have 30 after all the 3 rotations in the order {0,2} {1,4} {0,3}.

We performed {0,2} on A and now we have a new array A1.

We performed {1,4} on A1 and now we have a new array A2.

We performed {0,3} on A2 and now we have a new array A3.

Now we are looking for the value at index 1 in A3.

But A3 is {0,3} done on A2.

So index 1 in A3 is index 0 in A2.

But A2 is {1,4} done on A1.

So index 0 in A2 is also index 0 in A1 as it does not lie in the range {1,4}.

But A1 is {0,2} done on A.

So index 0 in A1 is index 2 in A.

On observing it, we are going deeper into the previous rotations starting from the latest rotation.

{0,3}

|

{1,4}

|

{0,2}

This is the reason we are processing the rotations in reverse order.

Please note that we are not rotating the elements in the reverse order, just processing the index from reverse.

Because if we actually rotate in reverse order, we might get a completely different answer as in case of rotations the order matters. 

C++




// CPP code to rotate an array
// and answer the index query
#include <bits/stdc++.h>
using namespace std;
 
// Function to compute the element at
// given index
int findElement(int arr[], int ranges[][2],
               int rotations, int index)
{
    for (int i = rotations - 1; i >= 0; i--) {
 
        // Range[left...right]
        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
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
 
    // No. of rotations
    int rotations = 2;
 
    // Ranges according to 0-based indexing
    int ranges[rotations][2] = { { 0, 2 }, { 0, 3 } };
 
    int index = 1;
 
    cout << findElement(arr, ranges, rotations, index);
 
    return 0;
 
}


Output

3

Time Complexity: O(N), where N represents the given number of rotations.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Please refer complete article on Find element at given index after a number of rotations for more details!
 



Last Updated : 10 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads