Python3 Program to Find the Mth element of the Array after K left rotations Last Updated : 06 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 1Output: 5Explanation: 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 = 2Output: 5 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: 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.The Mth element of the array after K left rotations is{ (K + M - 1) % N }th element in the original array. 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 %= 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 Output5Time 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! Comment More infoAdvertise with us Next Article Python3 Program to Find the Mth element of the Array after K left rotations K kartik Follow Improve Article Tags : Python rotation school-programming Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Types of Operating Systems Operating Systems can be categorized according to different criteria like whether an operating system is for mobile devices (examples Android and iOS) or desktop (examples Windows and Linux). Here, we are going to classify based on functionalities an operating system provides.8 Main Operating System 11 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Like