📌  相关文章
📜  数组右旋 K 次后的第 M 个元素

📅  最后修改于: 2021-09-07 02:03:57             🧑  作者: Mango

给定非负整数KM和由N 个元素组成的数组arr[ ] ,任务是在K 次右旋转后找到数组的M元素。

例子:

天真的方法:
解决问题最简单的方法是进行K次右旋操作,然后找到最终数组的M元素
时间复杂度: O(N * K)
辅助空间: O(N)
有效的方法:
为了优化问题,需要进行以下观察:

  • 如果数组旋转N次,它会再次返回初始数组。
  • 因此,经过K旋转后的数组中的元素与原始数组中索引为K%N处的元素相同。
  • 如果K >= M ,则 K 次右旋后数组的第 M 个元素为
  • 如果K < M ,则 K 次右旋后数组的第 M 个元素为:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
// Function to return Mth element of
// array after k right rotations
int getFirstElement(int a[], int N,
                    int K, int M)
{
    // The array comes to original state
    // after N rotations
    K %= N;
    int index;
 
    // If K is greater or equal to M
    if (K >= M)
 
        // Mth element after k right
        // rotations is (N-K)+(M-1) th
        // element of the array
        index = (N - K) + (M - 1);
 
    // Otherwise
    else
 
        // (M - K - 1) th element
        // of the array
        index = (M - K - 1);
 
    int result = a[index];
 
    // Return the result
    return result;
}
 
// Driver Code
int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
   
    int N = sizeof(a) / sizeof(a[0]);
   
    int K = 3, M = 2;
   
    cout << getFirstElement(a, N, K, M);
   
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
  
// Function to return Mth element of
// array after k right rotations
static int getFirstElement(int a[], int N,
                           int K, int M)
{
    // The array comes to original state
    // after N rotations
    K %= N;
    int index;
  
    // If K is greater or equal to M
    if (K >= M)
  
        // Mth element after k right
        // rotations is (N-K)+(M-1) th
        // element of the array
        index = (N - K) + (M - 1);
  
    // Otherwise
    else
  
        // (M - K - 1) th element
        // of the array
        index = (M - K - 1);
  
    int result = a[index];
  
    // Return the result
    return result;
}
  
// Driver Code
public static void main(String[] args)
{
    int a[] = { 1, 2, 3, 4, 5 };
    
    int N = 5;
    
    int K = 3, M = 2;
    
    System.out.println(getFirstElement(a, N, K, M));
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 program to implement
# the above approach
 
# Function to return Mth element of
# array after k right rotations
def getFirstElement(a, N, K, M):
 
    # The array comes to original state
    # after N rotations
    K %= N
 
    # If K is greater or equal to M
    if (K >= M):
 
        # Mth element after k right
        # rotations is (N-K)+(M-1) th
        # element of the array
        index = (N - K) + (M - 1)
 
    # Otherwise
    else:
 
        # (M - K - 1) th element
        # of the array
        index = (M - K - 1)
 
    result = a[index]
 
    # Return the result
    return result
 
# Driver Code
if __name__ == "__main__":
     
    a = [ 1, 2, 3, 4, 5 ]
    N = len(a)
 
    K , M = 3, 2
 
    print( getFirstElement(a, N, K, M))
 
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to return Mth element of
// array after k right rotations
static int getFirstElement(int []a, int N,
                        int K, int M)
{
    // The array comes to original state
    // after N rotations
    K %= N;
    int index;
 
    // If K is greater or equal to M
    if (K >= M)
 
        // Mth element after k right
        // rotations is (N-K)+(M-1) th
        // element of the array
        index = (N - K) + (M - 1);
 
    // Otherwise
    else
 
        // (M - K - 1) th element
        // of the array
        index = (M - K - 1);
 
    int result = a[index];
 
    // Return the result
    return result;
}
 
// Driver Code
public static void Main()
{
    int []a = { 1, 2, 3, 4, 5 };
     
    int N = 5;
     
    int K = 3, M = 2;
     
    Console.Write(getFirstElement(a, N, K, M));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
4

时间复杂度: O(1)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live