- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# JavaScript Program to Find Mth element after K Right Rotations of an Array

We are writing a JavaScript program to find the mth element after k right rotations of an array. Firstly, we will take the input for the array, m and k. Then, we will use a loop to perform the right rotations. In each iteration of the loop, we will move the last element of the array to the first position. We will continue this loop for k times to get the rotated array. Finally, we will return the mth element of the rotated array as the result.

## Approach

The approach for finding the mth element after k right rotations of an array can be broken down as follows −

Calculate the actual position of the mth element after k rotations, which would be (m-k) % n, where n is the length of the array.

Check if the calculated position is negative, in which case it can be converted to a positive position by adding n to it.

Return the element at the calculated position in the array.

To optimize this solution, you can use the modulo operator to keep the calculated position within the bounds of the array, so you don't need to check for negative values.

The time complexity of this solution is O(1), as the calculation of the final position and the retrieval of the element at that position are both constant-time operations.

The space complexity is O(1), as no additional data structures are used in the solution.

## Example

Here is an example of a JavaScript program that finds the mth element after k right rotations of an array

function findElement(arr, k, m) { k = k % arr.length; // handling large k values return arr[(arr.length - k + m - 1) % arr.length]; } let arr = [1, 2, 3, 4, 5]; let k = 2; let m = 3; console.log(findElement(arr, k, m));

## Explanation

The

**findElement**function takes in an array**arr**, number of rotations**k**, and the mth element to find.The

**k = k % arr.length**line calculates the actual number of rotations performed on the array after handling large**k**values. This is done because rotating the array more than its length does not change its position, hence taking**k**modulo the length of the array gives the actual number of rotations performed.The line

**return arr[(arr.length - k + m - 1) % arr.length];**calculates the position of the mth element after k rotations. The expression**arr.length - k**gives the starting position of the array after k rotations, and then**+ m - 1**gives the position of the mth element, and finally, taking the modulo with the length of the array ensures that the position wraps around the end of the array if it goes beyond the bounds.Finally, the program calls the

**findElement**function and logs the result. In this case, the output would be 4.

- Related Articles
- JavaScript Program to Find element at given index after a number of rotations
- JavaScript Program for Range sum queries for anticlockwise rotations of Array by K indices
- Program to find ith element by rotating k times to right
- JavaScript Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- C++ Program to Find Largest Element of an Array
- Write a program in C++ to find the top K frequent element in an array of integers
- JavaScript: Computing the average of an array after mapping each element to a value
- JavaScript Program to Find k maximum elements of array in original order
- Write a program to find the index of particular element in an array in javascript?
- Recursive program to find an element in an array linearly.
- JavaScript Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- C++ Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Java program to find array sum using bitwise OR after splitting given array in two halves after K circular shifts
- Python Program to find largest element in an array
- Python program to find k'th smallest element in a 2D array