Java Program to Find the Mth element of the Array after K left rotations Last Updated : 14 Oct, 2022 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}st 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: 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 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! Comment More infoAdvertise with us Next Article Java Program to Find the Mth element of the Array after K left rotations K kartik Follow Improve Article Tags : Java Greedy Searching Mathematical Java Programs DSA Arrays rotation school-programming +5 More Practice Tags : ArraysGreedyJavaMathematicalSearching +1 More Similar Reads Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by " 5 min read Find Mth element after K Right Rotations of an Array Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 Output: 5 Explanation: The array after first right rotation a1[ ] = {23, 3, 4, 5} The array a 11 min read Java Program to Print array after it is right rotated K times Given an Array of size N and a values 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 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: 2 min read Java 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:Â So 4 min read Java 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 min read Like