Rotating an array in place - JavaScript

Question

Rotate a one-dimensional array of n elements to the right by k steps. For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d}.

Origin from: LeetCode


Solution

// Time complexity: O(n)
function reverseArr(arr, left, right) {
    while (left < right) {
        var temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
    return arr;
}

function rotate(arr, k) {
    var l = arr.length;
    arr = reverseArr(arr, 0, l - 1);
    arr = reverseArr(arr, 0, k - 1);
    arr = reverseArr(arr, k, l - 1);
    console.log(arr);
    return arr;
}

var arr = [1, 2, 3, 4, 5];
rotate(arr, 3);

Demo

Yang Zhao

Read more posts by this author.


Comment