Find the Mth element of the Array after K left rotations
Last Updated :
14 Oct, 2022
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}
1st 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)
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:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return Mth element of
// array after k left rotations
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
int main()
{
// Array initialization
int a[] = { 3, 4, 5, 23 };
// Size of the array
int N = sizeof(a) / sizeof(a[0]);
// Given K rotation and Mth element
// to be found after K rotation
int K = 2, M = 1;
// Function call
cout << getFirstElement(a, N, K, M);
return 0;
}
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
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
C#
// C# program for the above approach
using System;
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
Console.Write(getFirstElement(a, N, K, M));
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program for the above approach
// Function to return Mth element of
// array after k left rotations
function 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
var index = (K + M - 1) % N;
var result = a[index];
// Return the result
return result;
}
// Driver code
// Array initialization
var a = [ 3, 4, 5, 23 ];
// Size of the array
var N = a.length;
// Given K rotation and Mth element
// to be found after K rotation
var K = 2, M = 1;
// Function call
document.write(getFirstElement(a, N, K, M));
// This code contributed by gauravrajput1
</script>
Time complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Javascript Program to Find the Mth element of the Array after K left rotations 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[ ]
2 min read
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
Find the index of the array elements after performing given operations K times Given an array arr[] and an integer K, the task is to print the position of the array elements, where the ith value in the result is the index of the ith element in the original array after applying following operations exactly K times: Remove the first array element and decrement it by 1.If it is g
7 min read
Javascript Program to 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
8 min read
Find element at given index after a number of rotations Given an array of N integers, M ranges of indices as queries, and a specific index K, the task is to right rotate the array circularly between query ranges, and return the element at Kth index in the final array. Examples: Input: arr[] : {1, 2, 3, 4, 5}, M = 2, queries[] = { {0, 2}, {0, 3} }, K = 1O
13 min read