📌  相关文章
📜  经过多次旋转后在给定索引处查找元素

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

经过多次旋转后在给定索引处查找元素

给出一个由 N 个整数组成的数组。我们执行了几个范围 [L..R] 的右圆周旋转。执行这些旋转后,我们需要在给定索引处找到元素。
例子 :

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. 

方法:蛮力蛮力方法是实际旋转所有给定范围的数组,最后返回修改后数组中给定索引处的元素。
方法:高效我们可以在保存所有范围后进行离线处理。
假设,我们的旋转范围是:[0..2] 和 [0..3]
我们从反向运行这些范围。
在范围 [0..3] 之后,索引 0 将包含索引 3 上的元素。
因此,我们可以将 0 更改为 3,即如果 index = left,则 index 将更改为 right。
在范围 [0..2] 之后,索引 3 将保持不受影响。
所以,我们可以做3种情况:
如果 index = left,则 index 将更改为右。
如果 index 不受范围限制,则没有旋转效果。
如果 index 在范围内,则 index 将具有 index-1 处的元素。
下面是实现:

为了更好的解释:-

10 20 30 40 50

索引:1

旋转:{0,2} {1,4} {0,3}

答案:索引 1 在按 {0,2} {1,4} {0,3} 的顺序所有 3 次旋转后将有 30。

我们在 A 上执行了 {0,2},现在我们有了一个新数组 A1。

我们在 A1 上执行了 {1,4},现在我们有了一个新数组 A2。

我们在 A2 上执行了 {0,3},现在我们有了一个新数组 A3。

现在我们正在寻找 A3 中索引 1 处的值。

但是 A3 是在 A2 上完成的 {0,3}。

所以 A3 中的索引 1 是 A2 中的索引 0。

但是 A2 是在 A1 上完成的 {1,4}。

所以 A2 中的索引 0 也是 A1 中的索引 0,因为它不在 {1,4} 范围内。

但是 A1 是在 A 上完成的 {0,2}。

所以 A1 中的索引 0 是 A 中的索引 2。

在观察它时,我们将从最近的轮换开始更深入地了解之前的轮换。

{0,3}

|

{1,4}

|

{0,2}

这就是我们以相反顺序处理旋转的原因。

请注意,我们没有以相反的顺序旋转元素,只是从反向处理索引。

因为如果我们实际上以相反的顺序旋转,我们可能会得到一个完全不同的答案,因为在旋转的情况下,顺序很重要。

C++
// CPP code to rotate an array
// and answer the index query
#include 
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
= 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
?>


Javascript


输出 :

3