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.
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 :
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; } |
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 ?> |
Output :
3
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the Mth element of the Array after K left rotations
- Mth element after K Right Rotations of an Array
- Find element at given index after given range reversals
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Maximum contiguous 1 possible in a binary string after k rotations
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Most frequent element in Array after replacing given index by K for Q queries
- Count of contiguous subarrays possible for every index by including the element at that index
- Farthest index that can be reached from the Kth index of given array by given operations
- Queries for number of distinct elements from a given index till last index in an array
- Quickly find multiple left rotations of an array | Set 1
- Maximum sum of i*arr[i] among all rotations of a given array
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Print all possible rotations of a given Array
- Find the index which is the last to be reduced to zero after performing a given operation
- Generate all rotations of a number
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Check if it is possible to reach to the index with value K when start index is given

