Python3 Program to Find the Mth element of the Array after K left rotations Last Updated : 06 Sep, 2024 Comments Improve Suggest changes 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 kartik Follow Improve Article Tags : Greedy Searching Mathematical Python Python Programs DSA Arrays rotation school-programming +5 More Practice Tags : ArraysGreedyMathematicalpythonSearching +1 More Similar Reads 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 1 min read Python3 Program for Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1Output : 3Explanation : 6 min read Python3 Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples:Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations.Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: Sort 4 min read Python Program to Print array after it is right rotated K times Given an Array of size N and a value K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples : Input: Array[] = {1, 3, 5, 7, 9}, K = 2.Output: 7 9 1 3 5Explanation:After 1st rotation - {9, 1, 3, 5, 7}After 2nd rotation - {7, 9, 1, 3, 5}Input: Array[] 2 min read Python3 Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat 3 min read Python3 Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 2 min read Python Program to Find the Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1.Examples: Input: arr[] = {8, 3, 1, 2}Output: 29Explanation: Lets look at all the rotations,{8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11{3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 8*3 = 5 min read Python Program to find minimum number of rotations to obtain actual string Given two strings s1 and s2. The task is to find out the minimum number of string rotations for the given string s1 to obtain the actual string s2. Examples: Input : eeksg, geeks Output: 1 Explanation: g is rotated left to obtain geeks. Input : eksge, geeks Output: 2 Explanation : e and g are left r 2 min read Python3 Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. Examp 8 min read Python3 Program to Modify given array to a non-decreasing array by rotation Given an array arr[] of size N (consisting of duplicates), the task is to check if the given array can be converted to a non-decreasing array by rotating it. If it's not possible to do so, then print "No". Otherwise, print "Yes".Examples:Input: arr[] = {3, 4, 5, 1, 2}Output: YesExplanation: After 2 3 min read Like