Skip to content
Related Articles

Related Articles

Find element at given index after a number of rotations

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 17 Jun, 2022

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 an 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, and finally, return the element at the given index in the modified array.
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 the 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, the index will be changed to right. 
After range [0..2], index 3 will remain unaffected.
So, we can make 3 cases : 
If index = left, the index will be changed to right. 
If the index is not bounded by the range, no effect of rotation. 
If the index is in bounds, the 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 the reverse.

Because if we actually rotate in reverse order, we might get a completely different answer as in the 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;
 
}

C




// C code to rotate an array
// and answer the index query
#include <stdio.h>
 
// 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[2][2] = { { 0, 2 }, { 0, 3 } };
 
    int index = 1;
 
    printf("%d",
           findElement(arr, ranges, rotations, index));
 
    return 0;
}
 
// This code is contributed by Deepthi

Java




// Java code to rotate an array
// and answer the index query
import java.util.*;
 
class GFG
{
    // Function to compute the element at
    // given index
    static int findElement(int[] arr, int[][] ranges,
                            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
    public static void main (String[] args) {
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // No. of rotations
        int rotations = 2;
     
        // Ranges according to 0-based indexing
        int[][] ranges = { { 0, 2 }, { 0, 3 } };
 
        int index = 1;
        System.out.println(findElement(arr, ranges,
                                rotations, index));
    }
}
 
/* This code is contributed by Mr. Somesh Awasthi */

Python3




# Python 3 code to rotate an array
# and answer the index query
 
# Function to compute the element
# at given index
def findElement(arr, ranges, rotations, index) :
     
    for i in range(rotations - 1, -1, -1 ) :
     
        # Range[left...right]
        left = ranges[i][0]
        right = ranges[i][1]
 
        # Rotation will not have
        # any effect
        if (left <= index and right >= index) :
            if (index == left) :
                index = right
            else :
                index = index - 1
         
    # Returning new element
    return arr[index]
 
# Driver Code
arr = [ 1, 2, 3, 4, 5 ]
 
# No. of rotations
rotations = 2
 
# Ranges according to
# 0-based indexing
ranges = [ [ 0, 2 ], [ 0, 3 ] ]
 
index = 1
 
print(findElement(arr, ranges, rotations, index))
     
# This code is contributed by Nikita Tiwari.

C#




// C# code to rotate an array
// and answer the index query
using System;
 
class GFG
{
    // Function to compute the
    // element at given index
    static int findElement(int []arr, int [,]ranges,
                        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 Code
    public static void Main ()
    {
        int []arr = { 1, 2, 3, 4, 5 };
 
        // No. of rotations
        int rotations = 2;
     
        // Ranges according
        // to 0-based indexing
        int [,]ranges = { { 0, 2 },
                        { 0, 3 } };
 
        int index = 1;
        Console.Write(findElement(arr, ranges,
                                    rotations,
                                    index));
    }
}
 
// This code is contributed
// by nitin mittal.

PHP




<?php
// PHP code to rotate an array
// and answer the index query
 
// Function to compute the
// element at given index
function findElement($arr, $ranges,
                    $rotations, $index)
{
    for ($i = $rotations - 1;
        $i >= 0; $i--)
    {
 
        // Range[left...right]
        $left = $ranges[$i][0];
        $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
$arr = array(1, 2, 3, 4, 5);
 
// No. of rotations
$rotations = 2;
 
// Ranges according
// to 0-based indexing
$ranges = array(array(0, 2),
                array(0, 3));
 
$index = 1;
 
echo findElement($arr, $ranges,
                $rotations, $index);
 
// This code is contributed by ajit
?>

Javascript




<script>
 
// JavaScript code to rotate an array
// and answer the index query
 
// Function to compute the element at
// given index
let findElement = (arr, ranges,rotations,index)=>{
    for (let i = rotations - 1; i >= 0; i--) {
        // Range[left...right]
        let left = ranges[i][0];
        let 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
let arr = [ 1, 2, 3, 4, 5 ];
 
// No. of rotations
let rotations = 2;
 
// Ranges according to 0-based indexing
let ranges = [ [ 0, 2 ], [ 0, 3] ];
 
let index = 1;
 
document.write(findElement(arr, ranges, rotations, index));
 
</script>

Output : 

3

Time Complexity: O(R), R is the number of rotations

Auxiliary Space: O(1),

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!