Right Rotate Array in JavaScript

medium

By - Aman Pareek

Last Updated - 27/08/2024

Problem Statement

right Rotate Array in JavaScript

Example 1

Input: array = [1, 2, 3, 4, 5] , position = 2

Output: [4, 5, 1, 2, 3]

Example 2

Input: array = ['a', 'b', 'c', 'd'] , position = 3

Output: ['b', 'c', 'd', 'a']

Example 3

Input: array = ['b', 'c', 'd', 'a'] , position = -4

Output: Throw error => Shift amount must be non-negative.

Solution 1: rotateArrayRight

function rotateArrayRight(arr, k) {
  if (!Array.isArray(arr)) {
    throw new Error("Input must be an array.");
  }
  if (k < 0) {
    throw new Error("Shift amount must be non-negative.");
  }

  const len = arr.length;
  k = k % len;

  // Handle edge cases efficiently
  if (k === 0 || len === 0) {
    return arr;
  }

  // Use a more optimized approach for larger arrays
  if (len >= 2 * k) {
    const temp = arr.slice(len - k);
    arr.splice(len - k);
    arr.unshift(...temp);
  } else {
    // For smaller arrays, use the existing approach
    arr = arr.slice(-k).concat(arr.slice(0, -k));
  }

  return arr;
} 

const array1 = [1, 2, 3, 4, 5];
const position1 = 2;
rotateArrayRight(array1,position1);  //output: [4, 5, 1, 2, 3] 

const array2 = ['a', 'b', 'c', 'd'];
const position2 = 3;
rotateArrayRight(array2,position2);  //output: ['b', 'c', 'd', 'a'] 

const array3 = ['b', 'c', 'd', 'a'];
const position3 = -4;
rotateArrayRight(array3,position3);  //Throw error => Shift amount must be non-negative. 

Solution 2: FunctionalRotateRight:

function functionalRotateRight(arr, k) {
  if (!Array.isArray(arr)) {
    throw new Error("Input must be an array.");
  }
  if (k < 0) {
    throw new Error("Shift amount must be non-negative.");
  }

  return arr.reduce((acc, val, i) => {
    const newIndex = (i + k) % arr.length;
    acc[newIndex] = val;
    return acc;
  }, []);
} 

const array1 = [1, 2, 3, 4, 5];
const position1 = 2;
functionalRotateRight(array1,position1);  //output: [4, 5, 1, 2, 3] 

const array2 = ['a', 'b', 'c', 'd'];
const position2 = 3;
functionalRotateRight(array2,position2);  //output: ['b', 'c', 'd', 'a'] 

const array3 = ['b', 'c', 'd', 'a'];
const position3 = -4;
functionalRotateRight(array3,position3);  //Throw error => Shift amount must be non-negative. 

Solution 3: ShiftArrayRight:

function shiftArrayRight(arr, k) {
  if (!Array.isArray(arr)) {
    throw new Error("Input must be an array.");
  }
  if (k < 0) {
    throw new Error("Shift amount must be non-negative.");
  }

  const n = arr.length;
  k %= n;
  for (let i = 0; i < k; i++) {
    const last = arr.pop();
    arr.unshift(last);
  }
  return arr;
} 

const array1 = [1, 2, 3, 4, 5];
const position1 = 2;
shiftArrayRight(array1,position1);  //output: [4, 5, 1, 2, 3] 

const array2 = ['a', 'b', 'c', 'd'];
const position2 = 3;
shiftArrayRight(array2,position2);  //output: ['b', 'c', 'd', 'a'] 

const array3 = ['b', 'c', 'd', 'a'];
const position3 = -4;
shiftArrayRight(array3,position3);  //Throw error => Shift amount must be non-negative. 

Popular Solutions