Java Program to Find the Mth element of the Array after K left rotations Last Updated : 14 Oct, 2022 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}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 rotation school-programming Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 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 Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application 8 min read Like