Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Instead of actually rotating the array, we can calculate the final position mathematically for optimal performance.
Understanding Right Rotations
In a right rotation, elements move to the right, and the last element wraps around to the first position. For example, rotating [1, 2, 3, 4, 5] right by 2 positions gives [4, 5, 1, 2, 3].
Mathematical Approach
The key insight is that we don't need to perform actual rotations. We can calculate where the mth element will be after k right rotations using this formula:
Calculate the effective rotations:
k % array.length(handles cases where k > array length)Find the new position:
(array.length - k + m - 1) % array.lengthReturn the element at that calculated position
Example
function findElement(arr, k, m) {
// Handle cases where k is larger than array length
k = k % arr.length;
// Calculate the position of mth element after k right rotations
let position = (arr.length - k + m - 1) % arr.length;
return arr[position];
}
// Test with example array
let arr = [1, 2, 3, 4, 5];
let k = 2; // Number of right rotations
let m = 3; // Find 3rd element (1-indexed)
console.log("Original array:", arr);
console.log("After", k, "right rotations, element at position", m, "is:", findElement(arr, k, m));
// Additional test cases
console.log("Position 1 after 2 rotations:", findElement(arr, 2, 1));
console.log("Position 5 after 3 rotations:", findElement(arr, 3, 5));
Original array: [ 1, 2, 3, 4, 5 ] After 2 right rotations, element at position 3 is: 1 Position 1 after 2 rotations: 4 Position 5 after 3 rotations: 2
Step-by-Step Explanation
The
findElementfunction takes an arrayarr, number of rotationsk, and positionmto findk = k % arr.lengthoptimizes for large k values since rotating an array by its length returns it to original state-
The formula
(arr.length - k + m - 1) % arr.lengthcalculates the final position:-
arr.length - kgives the offset caused by k right rotations -
+ m - 1adds the mth position (adjusted for 0-based indexing) -
% arr.lengthensures the position wraps around if it exceeds array bounds
-
Visualization Example
For array [1, 2, 3, 4, 5] with k=2 right rotations:
Original: [1, 2, 3, 4, 5] positions: [0, 1, 2, 3, 4] 1 rotation: [5, 1, 2, 3, 4] 2 rotations: [4, 5, 1, 2, 3] final result Element at position 3 (1-indexed) = element at index 2 = 1
Time and Space Complexity
Time Complexity: O(1) - constant time calculation
Space Complexity: O(1) - no additional data structures needed
Conclusion
This mathematical approach efficiently finds the mth element after k right rotations without actually performing the rotations. The formula handles edge cases and provides optimal O(1) time complexity.
