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 element at given index after a number of rotations
When working with arrays in JavaScript, we often need to find elements after rotating the array. A rotation shifts all elements in a specific direction. This program demonstrates how to efficiently find an element at a given index after performing a number of right rotations without actually rotating the array.
Understanding Array Rotations
A right rotation shifts all elements one position to the right, with the last element moving to the first position. For example, rotating [1, 2, 3, 4, 5] right by 2 positions gives [4, 5, 1, 2, 3].
Optimized Approach
Instead of performing actual rotations, we can calculate the new position mathematically using the modulo operator. This gives us O(1) time complexity.
Formula
For right rotations: newIndex = (originalIndex - rotations + arrayLength) % arrayLength
Example Implementation
function findElementAfterRotations(arr, rotations, index) {
// Normalize rotations to avoid unnecessary full rotations
rotations = rotations % arr.length;
// Calculate new index after right rotations
let newIndex = (arr.length + index - rotations) % arr.length;
return arr[newIndex];
}
// Example usage
let arr = [1, 2, 3, 4, 5];
let rotations = 2;
let index = 4;
console.log("Original array:", arr);
console.log("Element at index", index, "after", rotations, "rotations:",
findElementAfterRotations(arr, rotations, index));
// Test with different scenarios
console.log("Element at index 0 after 3 rotations:",
findElementAfterRotations(arr, 3, 0));
console.log("Element at index 2 after 7 rotations:",
findElementAfterRotations(arr, 7, 2));
Original array: [ 1, 2, 3, 4, 5 ] Element at index 4 after 2 rotations: 3 Element at index 0 after 3 rotations: 3 Element at index 2 after 7 rotations: 5
Step-by-Step Explanation
Normalize rotations:
rotations % arr.lengthremoves unnecessary full rotations since rotating by array length returns to original position.Calculate new index: We subtract rotations from the original index and add array length to handle negative results.
Apply modulo: Ensures the result stays within valid array bounds.
Return element: Access the element at the calculated new index.
Verification Example
// Let's verify with manual rotation
function manualRotation(arr, rotations) {
let result = [...arr]; // Create a copy
for (let i = 0; i
Manual rotation result: [ 4, 5, 1, 2, 3 ]
Element at index 4 in rotated array: 3
Our formula result: 3
Time Complexity Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Manual Rotation | O(n × k) | O(n) | Understanding concept |
| Mathematical Formula | O(1) | O(1) | Production code |
Conclusion
Using the mathematical approach with modulo operations provides an efficient O(1) solution for finding elements after rotations. This eliminates the need for actual array manipulation and significantly improves performance for large arrays or multiple queries.
