DEV Community

codingpineapple
codingpineapple

Posted on

LeetCode 189. Rotate Array (javascript solution)

Description:

Given an array, rotate the array to the right by k steps, where k is non-negative.

Solution:

Time Complexity : O(n)
Space Complexity: O(1)

var rotate = function(nums, k) {
    // Account for all possible k values
    k %= nums.length;
    // Rotate all the numbers
    reverse(nums, 0, nums.length - 1);
    // Rotate first k numbers
    reverse(nums, 0, k - 1);
    // Rotate number from k onward
    reverse(nums, k, nums.length - 1);

  // Function to rotate the array from params start to end
  function reverse(nums, start, end) {
    while (start < end) {
      const temp = nums[start];
      nums[start] = nums[end];
      nums[end] = temp;
      start++;
      end--;
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)