Finding the rotation of an array in JavaScript

We are required to write a JavaScript function that takes in an array and a number n.

Our function should rotate the array by n elements, i.e., take n elements from the front and put them to the end.

The only condition here is that we have to do this without using any extra space in memory.

For example, if the input array is:

const arr = [12, 6, 43, 5, 7, 2, 5];

and number n is 3, then the output should be:

const output = [5, 7, 2, 5, 12, 6, 43];

Understanding Array Rotation

Array rotation means moving elements from one position to another. When we rotate left by n positions, the first n elements move to the end, and remaining elements shift left.

Left Rotation by 3 positions Original: 12 6 43 5 7 2 5 Rotated: 5 7 2 5 12 6 43 First 3 elements moved to end Remaining elements shifted left

Method 1: Using Individual Rotations

This approach rotates the array one position at a time, repeating n times:

const arr = [12, 6, 43, 5, 7, 2, 5];
const num = 3;

const rotateByOne = arr => {
    let temp = arr[0];
    for(let i = 0; i < arr.length - 1; i++){
        arr[i] = arr[i + 1];
    }
    arr[arr.length - 1] = temp;
}

const rotateLeft = (arr, n) => {
    const length = arr.length;
    if(n >= length) {
        n = n % length; // Handle cases where n > array length
    }
    
    for(let i = 0; i < n; i++){
        rotateByOne(arr);
    }
    return arr;
}

console.log("Original array:", [12, 6, 43, 5, 7, 2, 5]);
console.log("After rotating left by", num, "positions:", rotateLeft([...arr], num));
Original array: [12, 6, 43, 5, 7, 2, 5]
After rotating left by 3 positions: [5, 7, 2, 5, 12, 6, 43]

Method 2: Using Array Reversal (Efficient)

A more efficient approach uses the reversal technique with O(n) time complexity:

const rotateLeftEfficient = (arr, n) => {
    const length = arr.length;
    n = n % length; // Handle cases where n > array length
    
    if(n === 0) return arr;
    
    // Helper function to reverse array elements between indices
    const reverse = (start, end) => {
        while(start < end) {
            let temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }
    
    // Step 1: Reverse first n elements
    reverse(0, n - 1);
    
    // Step 2: Reverse remaining elements
    reverse(n, length - 1);
    
    // Step 3: Reverse entire array
    reverse(0, length - 1);
    
    return arr;
}

const testArray = [12, 6, 43, 5, 7, 2, 5];
console.log("Original array:", testArray);
console.log("Rotated array:", rotateLeftEfficient([...testArray], 3));
Original array: [12, 6, 43, 5, 7, 2, 5]
Rotated array: [5, 7, 2, 5, 12, 6, 43]

Comparison

Method Time Complexity Space Complexity Approach
Individual Rotations O(n × k) O(1) Rotate one position k times
Reversal Technique O(n) O(1) Use three reversals

Key Points

  • Handle edge cases where rotation count exceeds array length using modulo operation
  • The reversal method is more efficient for large arrays and rotation counts
  • Both methods modify the original array in-place, using no extra memory

Conclusion

Array rotation can be achieved efficiently using the reversal technique with O(n) time complexity. For small arrays, the simple rotation method works fine, but the reversal approach scales better for larger datasets.

Updated on: 2026-03-15T23:18:59+05:30

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements