Rotate an Array to the Right by K Steps using JavaScript
Last Updated :
12 Apr, 2024
One can Rotate an array to the left by k steps using JavaScript. We are given an input array and a value k we have to rotate that array by k steps in the right direction.
Example:
Input
arr = [1 , 2 , 3 , 4 , 5 ]
k = 3
Output
arr = [ 3 , 4 , 5 , 1 , 2]
Below are different approaches to Rotate an array to the left by k steps using JavaScript which are as follows:
Using Reverse Method
To handle the case when k is greater than length of array , we will calculate k modulo the length of the array. Now Reverse the entire array. Reverse the first k elements of the array, where k is number of steps we have to rotate array in right direction. Reverse the remaining elements of the array. Return modified array.
Example: To demonstrate Rotation of an array to the left by k steps using reverse method only
JavaScript
function rotateArrayReverse(nums, k) {
const n = nums.length;
k = k % n;
// Reverse the entire array
nums.reverse();
// Reverse the first k elements
reverseArray(nums, 0, k - 1);
// Reverse the remaining elements
reverseArray(nums, k, n - 1);
return nums;
}
function reverseArray(arr, start, end) {
while (start < end) {
const temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
const nums = [1, 2, 3, 4, 5, 6, 7];
const k = 3;
console.log(rotateArrayReverse(nums, k));
Output[
5, 6, 7, 1,
2, 3, 4
]
Time complexity : O(n)
Space complexity : O(1)
Using Array Splice and Push
To handle the case when k is greater than length of array , we will calculate k modulo the length of the array. Now Use the splice method to remove the last k elements from the array and Push the removed elements at the beginning of the array. Return the modified array.
Example: To demonstrate Rotation of an array to the left by k steps using array splice and push
JavaScript
function rotateArraySplice(nums, k) {
const n = nums.length;
k = k % n;
// Remove the last k
// elements from array
const removed = nums.splice(-k);
// Insert the removed elements
// at starting of the array
nums.unshift(...removed);
return nums;
}
const nums = [1, 2, 3, 4, 5, 6, 7];
const k = 3;
console.log(rotateArraySplice(nums, k));
Output[
5, 6, 7, 1,
2, 3, 4
]
Time complexity : O(k)
Space complexity : O(k)
Using Slice and Concatenation
To handle the case when k is greater than length of array , we will calculate k modulo the length of the array. Now Use the slice() method to extract the last k elements and the remaining elements of the array. Concatenate the sliced parts: Concatenate the sliced parts in reverse order using the concat() method. Return the modified array
Example : To demonstrate Rotation of an array to the right by k steps using array splice and push
JavaScript
function rotateArraySliceConcat(nums, k) {
const n = nums.length;
k %= n;
// Concatenate the sliced parts in reverse order
const rotated = nums.slice(-k).concat(nums.slice(0, n - k));
return rotated;
}
const nums = [1, 2, 3, 4, 5, 6, 7];
const k = 3;
console.log(rotateArraySliceConcat(nums, k));
Output[
5, 6, 7, 1,
2, 3, 4
]
Time Complexity: O(k + m)
Space Complexity: O(k + m)
Share your thoughts in the comments
Please Login to comment...