📌  相关文章
📜  在给定范围反转后在给定索引处查找元素

📅  最后修改于: 2022-05-13 01:57:50.091000             🧑  作者: Mango

在给定范围反转后在给定索引处查找元素

给出一个由 N 个元素组成的数组。我们在独特的范围内进行了几次反转[L..R]。任务是打印给定索引处的元素。
例子:

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

蛮力方法实际上是反转一系列元素并在之后回答查询。
有效方法:如果我们观察,范围[L..R]的反转将导致如下:
索引将变为right + left – index
通过这样做,我们可以轻松计算索引。

C++
// Program to find index of an element after
// given range reversals.
#include 
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;
}


Java
// 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 */


Python3
# 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


C#
// 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.


PHP
= 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.
?>


Javascript


输出:

50