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++
#include <bits/stdc++.h>
using namespace std;
int findElement(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];
if (left <= index && right >= index) {
if (index == left)
index = right;
else
index--;
}
}
return arr[index];
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int rotations = 2;
int ranges[rotations][2] = { { 0, 2 }, { 0, 3 } };
int index = 1;
cout << findElement(arr, ranges, rotations, index);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findElement(int[] arr, int[][] ranges,
int rotations, int index)
{
for (int i = rotations - 1; i >= 0; i--) {
int left = ranges[i][0];
int right = ranges[i][1];
if (left <= index && right >= index) {
if (index == left)
index = right;
else
index--;
}
}
return arr[index];
}
public static void main (String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
int rotations = 2;
int[][] ranges = { { 0, 2 }, { 0, 3 } };
int index = 1;
System.out.println(findElement(arr, ranges,
rotations, index));
}
}
|
Python3
def findElement(arr, ranges, rotations, index) :
for i in range(rotations - 1, -1, -1 ) :
left = ranges[i][0]
right = ranges[i][1]
if (left <= index and right >= index) :
if (index == left) :
index = right
else :
index = index - 1
return arr[index]
arr = [ 1, 2, 3, 4, 5 ]
rotations = 2
ranges = [ [ 0, 2 ], [ 0, 3 ] ]
index = 1
print(findElement(arr, ranges, rotations, index))
|
C#
using System;
class GFG
{
static int findElement(int []arr, int [,]ranges,
int rotations, int index)
{
for (int i = rotations - 1; i >= 0; i--)
{
int left = ranges[i, 0];
int right = ranges[i, 1];
if (left <= index &&
right >= index)
{
if (index == left)
index = right;
else
index--;
}
}
return arr[index];
}
public static void Main ()
{
int []arr = { 1, 2, 3, 4, 5 };
int rotations = 2;
int [,]ranges = { { 0, 2 },
{ 0, 3 } };
int index = 1;
Console.Write(findElement(arr, ranges,
rotations,
index));
}
}
|
PHP
<?php
function findElement($arr, $ranges,
$rotations, $index)
{
for ($i = $rotations - 1;
$i >= 0; $i--)
{
$left = $ranges[$i][0];
$right = $ranges[$i][1];
if ($left <= $index &&
$right >= $index)
{
if ($index == $left)
$index = $right;
else
$index--;
}
}
return $arr[$index];
}
$arr = array(1, 2, 3, 4, 5);
$rotations = 2;
$ranges = array(array(0, 2),
array(0, 3));
$index = 1;
echo findElement($arr, $ranges,
$rotations, $index);
?>
|
Javascript
<script>
let findElement = (arr, ranges,rotations,index)=>{
for (let i = rotations - 1; i >= 0; i--) {
let left = ranges[i][0];
let right = ranges[i][1];
if (left <= index && right >= index) {
if (index == left)
index = right;
else
index--;
}
}
return arr[index];
}
let arr = [ 1, 2, 3, 4, 5 ];
let rotations = 2;
let ranges = [ [ 0, 2 ], [ 0, 3] ];
let index = 1;
document.write(findElement(arr, ranges, rotations, index));
</script>
|
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.