Wednesday, July 3, 2024
HomeLanguagesPythonPython3 Program to Find the Mth element of the Array after K...

Python3 Program to Find the Mth element of the Array after K left rotations

Given non-negative integers K, M, and an array arr[] with N elements find the Mth element of the array after K left rotations.

Examples:

Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1
Output: 5
Explanation: 
The array after first left rotation a1[ ] = {4, 5, 23, 3}
The array after second left rotation a2[ ] = {5, 23, 3, 4}
1st element after 2 left rotations is 5.

Input: arr[] = {1, 2, 3, 4, 5}, K = 3, M = 2
Output:
Explanation: 
The array after 3 left rotation has 5 at its second position.

Naive Approach: The idea is to Perform Left rotation operation K times and then find the Mth element of the final array.

Time Complexity: O(N * K)
Auxiliary Space: O(N)

    Efficient Approach: To optimize the problem, observe the following points:

  1. If the array is rotated N times it returns the initial array again.

    For example, a[ ] = {1, 2, 3, 4, 5}, K=5 then the array after 5 left rotation a5[ ] = {1, 2, 3, 4, 5}.

    Therefore, the elements in the array after Kth rotation is the same as the element at index K%N in the original array.

  2. The Mth element of the array after K left rotations is

    { (K + M – 1) % N }th

    element in the original array.

  3.  
    Below is the implementation of the above approach:

    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 %=
      
        # Mth element after k left rotations 
        # is (K+M-1)%N th element of the 
        # original array 
        index = (K + M - 1) %
      
        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 

    
    
    Output: 

    5

     

    Time complexity: O(1)
    Auxiliary Space: O(1)

    Please refer complete article on Find the Mth element of the Array after K left rotations for more details!

    Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
    You’ll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We’re here to prepare you thoroughly for online assessments and interviews.
    Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000 neveropen! Whether it’s DSA in C++, Java, Python, or JavaScript we’ve got you covered. Let’s embark on this exciting journey together!

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments