JavaScript Program to Find Circular Rotation of an Array by K Positions
Last Updated :
13 May, 2024
Given an array of integers and an integer K, rotate the array to the right by K positions in a circular manner.
Examples:
Input: nums = [1, 2, 3, 4, 5], k=2
Output: [4, 5, 1, 2, 3]
Input: nums = [6, 7, 8, 9, 10], k=1
Output: [10, 9, 6, 7, 8]
Below are the approaches to finding the Circular rotation of an array by K positions which are as follows:
We first copy the last K elements of the array into a separate array and then shift the remaining elements of the original array to the right by K positions and in the last we copy the rotated elements back into the original array.
Example: Implementation of program to Find Circular rotation of an array by K positions using Extra Space
JavaScript
let nums = [1, 2, 3, 4, 5];
let k = 2;
function rotate(nums, k) {
const rotation = nums.slice(-k);
nums.splice(-k);
nums.unshift(...rotation);
return nums;
}
rotate(nums, k);
console.log(nums);
OutputOriginal Array: [ 1, 2, 3, 4, 5 ]
Circularly rotated Array: [ 4, 5, 1, 2, 3 ]
Time Complexity: O(n), where ?n is the number of elements in the array.
Auxiliary Space: O(k), where K is the number of elements to rotate.
Reverse Technique
In this approach, we use the reverse technique to rotate the array circularly by k positions to the right. It first reverses the entire array, then reverses the first k elements, and finally reverses the remaining elements.
Example: Implementation of program to Find Circular rotation of an array by K positions using Reverse Technique.
JavaScript
function reverse(arr, start, end) {
while (start < end) {
const temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
function rotateCircularly(arr, k) {
const n = arr.length;
reverse(arr, 0, n - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
return arr;
}
const arr = [1, 2, 3, 4, 5];
const k = 2;
console.log("Original Array:", arr);
console.log("Circularly rotated Array:", rotateCircularly(arr, k));
OutputOriginal Array: [ 1, 2, 3, 4, 5 ]
Circularly rotated Array: [ 4, 5, 1, 2, 3 ]
Time Complexity: O(N),where ?n is the number of elements in the array
Auxiliary Space: O(1)
Similar Reads
Javascript Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
3 min read
Javascript Program for Program to cyclically rotate an array by one Given an array, cyclically rotate the array clockwise by one. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: arr[] = {5, 1, 2, 3, 4}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Following are steps. 1) Store last element in a variable say x. 2) Shift all eleme
2 min read
Javascript Program to Find Mth element after K Right Rotations of an Array Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 Output: 5 Explanation: The array after first right rotation a1[ ] = {23, 3, 4, 5} The array a
8 min read
Javascript Program to Find the Mth element of the Array after K left rotations Given non-negative integers K, M, and an array arr[] with N elements find the Mth element of the array after K left rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1Output: 5Explanation:Â The array after first left rotation a1[ ] = {4, 5, 23, 3}The array after second left rotation a2[ ]
2 min read
Javascript Program to Check if all rows of a matrix are circular rotations of each other Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1Output: YesAll rows are rotated permutationof each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: NoExplanation : As 3, 2, 1 is not
2 min read
Javascript Program to Print array after it is right rotated K times Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples :Â Â Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input:
2 min read