JavaScript Program for Reversal algorithm for right rotation of an array

Right rotation of an array means shifting elements to the right by a given number of positions. Elements that move beyond the array's end wrap around to the beginning. We'll implement the reversal algorithm, which is an efficient approach for array rotation.

Example

Input array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Right rotations: 3
Output: [7, 8, 9, 1, 2, 3, 4, 5, 6]

How Right Rotation Works

Let's trace through 3 right rotations step by step:

  • Original: [1, 2, 3, 4, 5, 6, 7, 8, 9]

  • After 1st rotation: [9, 1, 2, 3, 4, 5, 6, 7, 8]

  • After 2nd rotation: [8, 9, 1, 2, 3, 4, 5, 6, 7]

  • After 3rd rotation: [7, 8, 9, 1, 2, 3, 4, 5, 6]

Reversal Algorithm Approach

Instead of rotating one by one, the reversal algorithm uses three reverse operations:

  1. Reverse the first (n-k) elements

  2. Reverse the last k elements

  3. Reverse the entire array

For our example with k=3:

  • Step 1: Reverse first 6 elements: [6, 5, 4, 3, 2, 1, 7, 8, 9]

  • Step 2: Reverse last 3 elements: [6, 5, 4, 3, 2, 1, 9, 8, 7]

  • Step 3: Reverse entire array: [7, 8, 9, 1, 2, 3, 4, 5, 6]

Implementation

// Function to reverse array elements within a range
function revArray(arr, left, right) {
    while (left < right) {
        let temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
    return arr;
}

// Function to perform right rotation using reversal algorithm
function rightRotate(arr, k) {
    let len = arr.length;
    
    // Handle edge cases
    if (k === 0 || len === 0) return arr;
    
    // Optimize rotations using modulo
    k = k % len;
    
    // Step 1: Reverse first (len-k) elements
    arr = revArray(arr, 0, len - k - 1);
    
    // Step 2: Reverse last k elements
    arr = revArray(arr, len - k, len - 1);
    
    // Step 3: Reverse entire array
    arr = revArray(arr, 0, len - 1);
    
    return arr;
}

// Function to print array elements
function printArray(arr) {
    console.log(arr.join(' '));
}

// Test the implementation
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let rotations = 3;

console.log("Original array:");
printArray(arr);

console.log(`Array after ${rotations} right rotations:`);
arr = rightRotate([...arr], rotations); // Use spread operator to avoid modifying original
printArray(arr);
Original array:
1 2 3 4 5 6 7 8 9
Array after 3 right rotations:
7 8 9 1 2 3 4 5 6

Time and Space Complexity

Complexity Value Explanation
Time O(n) Three reverse operations, each taking O(n/2)
Space O(1) Only uses a constant amount of extra space

Key Points

  • Use k % length to handle rotations larger than array size

  • The reversal algorithm is more efficient than rotating elements one by one

  • This approach works in-place without requiring additional arrays

  • The algorithm consists of exactly three reverse operations

Conclusion

The reversal algorithm provides an efficient O(n) time and O(1) space solution for right array rotation. By reversing specific segments and then the entire array, we achieve the desired rotation without needing extra memory or multiple iterations.

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

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements