📌  相关文章
📜  在 K 次左旋转后找到数组的第 M 个元素

📅  最后修改于: 2021-10-26 02:38:59             🧑  作者: Mango

给定非负整数KM和具有N 个元素的数组arr[]K 次左旋转后找到数组的M元素。

例子:

朴素的方法:想法是执行左旋转操作K次,然后找到最终数组的M元素

时间复杂度: O(N * K)
辅助空间: O(N)

    有效的方法:要优化问题,请注意以下几点:

  1. 如果数组旋转N次,它会再次返回初始数组。

    因此,经过第 K 次旋转后的数组中的元素与原始数组中索引K%N处的元素相同。

  2. K次左旋转后数组的第M个元素是

    原始数组中的元素。

     
    下面是上述方法的实现:

    C++
    // C++ program for the above approach 
    #include  
    using namespace std; 
      
    // Function to return Mth element of 
    // array after k left rotations 
    int getFirstElement(int a[], int N, 
                        int K, int M) 
    { 
        // The array comes to original state 
        // after N rotations 
        K %= N; 
      
        // Mth element after k left rotations 
        // is (K+M-1)%N th element of the 
        // original array 
        int index = (K + M - 1) % N; 
      
        int result = a[index]; 
      
        // Return the result 
        return result; 
    } 
      
    // Driver Code 
    int main() 
    { 
        // Array initialization 
        int a[] = { 3, 4, 5, 23 }; 
      
        // Size of the array 
        int N = sizeof(a) / sizeof(a[0]); 
      
        // Given K rotation and Mth element 
        // to be found after K rotation 
        int K = 2, M = 1; 
      
        // Function call 
        cout << getFirstElement(a, N, K, M); 
        return 0; 
    }


    Java
    // Java program for the above approach 
    import java.util.*; 
    class GFG{ 
          
    // Function to return Mth element of 
    // array after k left rotations 
    public static int getFirstElement(int[] a, int N, 
                                      int K, int M) 
    { 
          
        // The array comes to original state 
        // after N rotations 
        K %= N; 
      
        // Mth element after k left rotations 
        // is (K+M-1)%N th element of the 
        // original array 
        int index = (K + M - 1) % N; 
      
        int result = a[index]; 
      
        // Return the result 
        return result; 
    } 
      
    // Driver code 
    public static void main(String[] args) 
    { 
          
        // Array initialization 
        int a[] = { 3, 4, 5, 23 }; 
      
        // Size of the array 
        int N = a.length; 
      
        // Given K rotation and Mth element 
        // to be found after K rotation 
        int K = 2, M = 1; 
      
        // Function call 
        System.out.println(getFirstElement(a, N, K, M)); 
    } 
    } 
      
    // This code is contributed by divyeshrabadiya07


    Python3
    # Python3 program for the above approach 
      
    # Function to return Mth element of 
    # array after k left rotations 
    def getFirstElement(a, N, K, M): 
          
        # The array comes to original state 
        # after N rotations 
        K %= N 
      
        # Mth element after k left rotations 
        # is (K+M-1)%N th element of the 
        # original array 
        index = (K + M - 1) % N 
      
        result = a[index] 
      
        # Return the result 
        return result 
      
    # Driver Code 
    if __name__ == '__main__': 
          
        # Array initialization 
        a = [ 3, 4, 5, 23 ] 
      
        # Size of the array 
        N = len(a) 
      
        # Given K rotation and Mth element 
        # to be found after K rotation 
        K = 2
        M = 1
      
        # Function call 
        print(getFirstElement(a, N, K, M)) 
      
    # This code is contributed by mohit kumar 29


    C#
    // C# program for the above approach 
    using System;
      
    class GFG{
          
    // Function to return Mth element of 
    // array after k left rotations 
    public static int getFirstElement(int[] a, int N, 
                                      int K, int M) 
    { 
          
        // The array comes to original state 
        // after N rotations 
        K %= N; 
      
        // Mth element after k left rotations 
        // is (K+M-1)%N th element of the 
        // original array 
        int index = (K + M - 1) % N; 
      
        int result = a[index]; 
      
        // Return the result 
        return result; 
    } 
      
    // Driver code
    public static void Main(string[] args) 
    {
          
        // Array initialization 
        int []a = { 3, 4, 5, 23 }; 
      
        // Size of the array 
        int N = a.Length; 
      
        // Given K rotation and Mth element 
        // to be found after K rotation 
        int K = 2, M = 1; 
      
        // Function call 
        Console.Write(getFirstElement(a, N, K, M));
    }
    }
      
    // This code is contributed by rutvik_56


    Javascript


    输出:
    5

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