Find element at given index after given range reversals

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 afterwards.

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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to find index of an element after
// given range reversals.
#include <bits/stdc++.h>
using namespace std;
  
// Function to compute the element at query index
int answer(int arr[], int ranges[][2], int reversals,
           int index)
{
    for (int i = reversals - 1; i >= 0; i--) {
        // Range[left...right]
        int left = ranges[i][0], right = ranges[i][1];
  
        // If doesn't satisfy, reversal won't
        // have any effect
        if (left <= index && right >= index)
            index = right + left - index;
    }
  
    // Returning element at modified index
    return arr[index];
}
  
// Driver
int main()
{
    int arr[] = { 10, 20, 30, 40, 50 };
  
    // reversals
    int reversals = 2;
    int ranges[reversals][2] = { { 1, 4 }, { 0, 2 } };
  
    int index = 1;
    cout << answer(arr, ranges, reversals, index);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to find index of an element
// after given range reversals.
import java.util.Arrays;
  
class GFG {
    // Function to compute the element at
    // query index
    static int answer(int[] arr, int[][] ranges,
                      int reversals, int index)
    {
        for (int i = reversals - 1; i >= 0; i--) {
            // Range[left...right]
            int left = ranges[i][0],
                right = ranges[i][1];
  
            // If doesn't satisfy, reversal
            // won't have any effect
            if (left <= index && right >= index)
                index = right + left - index;
        }
  
        // Returning element at modified index
        return arr[index];
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int[] arr = { 10, 20, 30, 40, 50 };
  
        // reversals
        int reversals = 2;
        int[][] ranges = { { 1, 4 }, { 0, 2 } };
  
        int index = 1;
        System.out.println(answer(arr, ranges,
                                  reversals, index));
    }
}
  
/* This code is contributed by Mr. Somesh Awasthi */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to find index of an element 
# after given range reversals.
  
# Function to compute the element
# at query index
def answer(arr, ranges, reversals, index):
    i = reversals - 1
    while(i >= 0):
          
        # Range[left...right]
        left = ranges[i][0]
        right = ranges[i][1]
  
        # If doesn't satisfy, reversal won't
        # have any effect
        if (left <= index and right >= index):
            index = right + left - index
      
        i -= 1
      
    # Returning element at modified index
    return arr[index]
  
# Driver Code
if __name__ == '__main__':
    arr = [10, 20, 30, 40, 50]
  
    # reversals
    reversals = 2
    ranges = [ [ 1, 4 ], [ 0, 2 ] ]
  
    index = 1
    print(answer(arr, ranges,
                 reversals, index))
  
# This code is contributed by 
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find index of an element
// after given range reversals.
using System;
  
class GFG {
      
    // Function to compute the element at
    // query index
    static int answer(int[] arr, int[, ] ranges,
                       int reversals, int index)
    {
        for (int i = reversals - 1; i >= 0; i--)
        {
              
            // Range[left...right]
            int left = ranges[i, 0],
                right = ranges[i, 1];
  
            // If doesn't satisfy, reversal
            // won't have any effect
            if (left <= index && right >= index)
                index = right + left - index;
        }
  
        // Returning element at modified index
        return arr[index];
    }
  
    // Driver code
    public static void Main()
    {
  
        int[] arr = { 10, 20, 30, 40, 50 };
  
        // reversals
        int reversals = 2;
        int[, ] ranges = { { 1, 4 }, { 0, 2 } };
  
        int index = 1;
        Console.WriteLine(answer(arr, ranges,
                                reversals, index));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to find index 
// of an element after
// given range reversals.
  
// Function to compute the
// element at query index
function answer($arr, $ranges
                $reversals, $index)
{
    for ($i = $reversals - 1; 
              $i >= 0; $i--) 
    {
        // Range[left...right]
        $left = $ranges[$i][0]; 
        $right = $ranges[$i][1];
  
        // If doesn't satisfy,
        // reversal won't have 
        // any effect
        if ($left <= $index && 
            $right >= $index)
            $index = $right + $left
                              $index;
    }
  
    // Returning element
    // at modified index
    return $arr[$index];
}
  
// Driver Code
$arr = array( 10, 20, 30, 40, 50 );
  
// reversals
$reversals = 2;
$ranges = array(array( 1, 4 ), 
                array( 0, 2 ));
  
$index = 1;
echo answer($arr, $ranges,
            $reversals, $index);
  
// This code is contributed 
// by nitin mittal.
?>

chevron_right



Output :

50

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.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.