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.length

  • Return 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 findElement function takes an array arr, number of rotations k, and position m to find

  • k = k % arr.length optimizes for large k values since rotating an array by its length returns it to original state

  • The formula (arr.length - k + m - 1) % arr.length calculates the final position:

    • arr.length - k gives the offset caused by k right rotations
    • + m - 1 adds the mth position (adjusted for 0-based indexing)
    • % arr.length ensures 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.

Updated on: 2026-03-15T23:19:01+05:30

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements