Saturday, September 21, 2024
Google search engine
HomeLanguagesPython3 Program to Find Mth element after K Right Rotations of an...

Python3 Program to Find Mth element after K Right Rotations of an Array

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


Output

4

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

Please refer complete article on Mth element after K Right Rotations of an Array 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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments