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}
st element after 2 left rotations is 5.Input: arr[] = {1, 2, 3, 4, 5}, K = 3, M = 2
Output: 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)
- 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:C++
// C++ program for the above approach#include <bits/stdc++.h>usingnamespacestd;// Function to return Mth element of// array after k left rotationsintgetFirstElement(inta[],intN,intK,intM){// The array comes to original state// after N rotationsK %= N;// Mth element after k left rotations// is (K+M-1)%N th element of the// original arrayintindex = (K + M - 1) % N;intresult = a[index];// Return the resultreturnresult;}// Driver Codeintmain(){// Array initializationinta[] = { 3, 4, 5, 23 };// Size of the arrayintN =sizeof(a) /sizeof(a[0]);// Given K rotation and Mth element// to be found after K rotationintK = 2, M = 1;// Function callcout << getFirstElement(a, N, K, M);return0;}Java
// Java program for the above approachimportjava.util.*;classGFG{// Function to return Mth element of// array after k left rotationspublicstaticintgetFirstElement(int[] a,intN,intK,intM){// The array comes to original state// after N rotationsK %= N;// Mth element after k left rotations// is (K+M-1)%N th element of the// original arrayintindex = (K + M -1) % N;intresult = a[index];// Return the resultreturnresult;}// Driver codepublicstaticvoidmain(String[] args){// Array initializationinta[] = {3,4,5,23};// Size of the arrayintN = a.length;// Given K rotation and Mth element// to be found after K rotationintK =2, M =1;// Function callSystem.out.println(getFirstElement(a, N, K, M));}}// This code is contributed by divyeshrabadiya07Python3
# Python3 program for the above approach# Function to return Mth element of# array after k left rotationsdefgetFirstElement(a, N, K, M):# The array comes to original state# after N rotationsK%=N# Mth element after k left rotations# is (K+M-1)%N th element of the# original arrayindex=(K+M-1)%Nresult=a[index]# Return the resultreturnresult# Driver Codeif__name__=='__main__':# Array initializationa=[3,4,5,23]# Size of the arrayN=len(a)# Given K rotation and Mth element# to be found after K rotationK=2M=1# Function callprint(getFirstElement(a, N, K, M))# This code is contributed by mohit kumar 29C#
// C# program for the above approachusingSystem;classGFG{// Function to return Mth element of// array after k left rotationspublicstaticintgetFirstElement(int[] a,intN,intK,intM){// The array comes to original state// after N rotationsK %= N;// Mth element after k left rotations// is (K+M-1)%N th element of the// original arrayintindex = (K + M - 1) % N;intresult = a[index];// Return the resultreturnresult;}// Driver codepublicstaticvoidMain(string[] args){// Array initializationint[]a = { 3, 4, 5, 23 };// Size of the arrayintN = a.Length;// Given K rotation and Mth element// to be found after K rotationintK = 2, M = 1;// Function callConsole.Write(getFirstElement(a, N, K, M));}}// This code is contributed by rutvik_56Javascript
<script>// Javascript program for the above approach// Function to return Mth element of// array after k left rotationsfunctiongetFirstElement(a , N , K , M) {// The array comes to original state// after N rotationsK %= N;// Mth element after k left rotations// is (K+M-1)%N th element of the// original arrayvarindex = (K + M - 1) % N;varresult = a[index];// Return the resultreturnresult;}// Driver code// Array initializationvara = [ 3, 4, 5, 23 ];// Size of the arrayvarN = a.length;// Given K rotation and Mth element// to be found after K rotationvarK = 2, M = 1;// Function calldocument.write(getFirstElement(a, N, K, M));// This code contributed by gauravrajput1</script>Output:5
Time complexity: O(1)
Auxiliary Space: O(1)Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Efficient Approach: To optimize the problem, observe the following points:


