Find element at given index after given range reversals
Last Updated :
29 Oct, 2023
An array consisting of N elements is given. There are several reversals we do in unique ranges[L..R]. The task is to print the element at given index.
Examples:
Input :
arr[] : 10 20 30 40 50
ranges[] = {{1, 4}, {0, 2}}
Query Index = 1
Output : 50
Explanation :
Reverse range[1..4] : 10 50 40 30 20
Reverse range[0..2] : 40 50 10 30 20
So we have 50 at index 1
The Brute Force approach would be to actually reverse a range of elements and answer the queries afterward.
Efficient Method: If we observe, the reversal of range[L..R] will result as follows :
The index will become right + left – index.
By doing this, we can compute the index easily.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int answer( int arr[], int ranges[][2], int reversals,
int index)
{
for ( int i = reversals - 1; i >= 0; i--) {
int left = ranges[i][0], right = ranges[i][1];
if (left <= index && right >= index)
index = right + left - index;
}
return arr[index];
}
int main()
{
int arr[] = { 10, 20, 30, 40, 50 };
int reversals = 2;
int ranges[reversals][2] = { { 1, 4 }, { 0, 2 } };
int index = 1;
cout << answer(arr, ranges, reversals, index);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int answer( int [] arr, int [][] ranges,
int reversals, int index)
{
for ( int i = reversals - 1 ; i >= 0 ; i--) {
int left = ranges[i][ 0 ],
right = ranges[i][ 1 ];
if (left <= index && right >= index)
index = right + left - index;
}
return arr[index];
}
public static void main(String[] args)
{
int [] arr = { 10 , 20 , 30 , 40 , 50 };
int reversals = 2 ;
int [][] ranges = { { 1 , 4 }, { 0 , 2 } };
int index = 1 ;
System.out.println(answer(arr, ranges,
reversals, index));
}
}
|
Python3
def answer(arr, ranges, reversals, index):
i = reversals - 1
while (i > = 0 ):
left = ranges[i][ 0 ]
right = ranges[i][ 1 ]
if (left < = index and right > = index):
index = right + left - index
i - = 1
return arr[index]
if __name__ = = '__main__' :
arr = [ 10 , 20 , 30 , 40 , 50 ]
reversals = 2
ranges = [ [ 1 , 4 ], [ 0 , 2 ] ]
index = 1
print (answer(arr, ranges,
reversals, index))
|
C#
using System;
class GFG {
static int answer( int [] arr, int [, ] ranges,
int reversals, int index)
{
for ( int i = reversals - 1; i >= 0; i--)
{
int left = ranges[i, 0],
right = ranges[i, 1];
if (left <= index && right >= index)
index = right + left - index;
}
return arr[index];
}
public static void Main()
{
int [] arr = { 10, 20, 30, 40, 50 };
int reversals = 2;
int [, ] ranges = { { 1, 4 }, { 0, 2 } };
int index = 1;
Console.WriteLine(answer(arr, ranges,
reversals, index));
}
}
|
Javascript
PHP
Optimized solution
We can start by applying all the reversals to the array in the order they are given. To do this efficiently, we can use a helper function to reverse a given range of elements in the array. After applying all the reversals, we can answer the queries directly by accessing the element at the given index.
Algorithm
First define function reverseRange(arr, L, R) that takes an array arr such that
a.Two indices L and R as input.
If L < R, THAN swap the elements at indices L and R in the array arr.
After that Increment L and decrement R.
Define a function applyReversals(arr, ranges)
For each pair of indices (L, R) in ranges, call the reverseRange function with arguments (arr, L, R).
Define a function getElementAtIndex(arr, index)
Return the element of arr at index index.
Initialize an array arr and a list of ranges ranges.
Call the applyReversals and getElementAtIndex function
Assign the result to variable result.
Get result as output.
Implementation of above program
C++
Java
Python
C#
Javascript
Time complexity O(N*M), where N is the length of the input array arr and M is the number of ranges in the input ranges.
Space complexity is O(1),as it does not use any additional memory.
This article is contributed by Rohit Thapliyal.
Share your thoughts in the comments
Please Login to comment...