JavaScript Program for Program to cyclically rotate an array by one


Cyclic rotation means shifting the value present at each index to their left or right by one and for one corner it takes the value present at the other corner. For the left rotation the value present at the first index will go at the last index and for all indexes takes the value present at the index which is present after them. In the right rotation, the thing is exactly the opposite to the left rotation. In this article, we will see the proper code with implementation to perform the rotations on the array.

Introduction to Problem

In the given problem, we will be given an array and we have to return or print an array which is the cyclic rotation of the given array. Cyclic rotation could be left cyclic or right cyclic.

In the right cyclic rotation each element from the 1st index to the last index contains the value of the previous index and the 0th index contains the value that was present at the last index.

In the left cyclic rotation each element from the 0th index to the second last index contains the value of the next index and the last index contains the value that was present at the 0th index.

For example −

Given array: 1 2 3 4 5 6
Left rotation: 2 3 4 5 6 1
Right rotation: 6 1 2 3 4 5 

Let us move to the approaches to implementing the problem

Approach

In this program, we will traverse over the array and will assign the current element the value of the previous element in the right rotation and the value of the next element to the current element in the left rotation. We will store the values of the first and the last element in an extra variable to assign to the last and the first element in the left and right rotation respectively.

Example

// definging function to rotate the array in left direction 
function Left_rotate(arr){
   var n = arr.length
   var temp = arr[0]
   
   // traversing over the array 
   for(var i = 0; i < n-1; i++){
      arr[i] = arr[i+1];
   }
   arr[n-1] = temp;
   console.log("Array after left rotation is: " )
   console.log(arr)
}

// definging function to rotate the array in right direction 
function Right_rotate(arr){
   var n = arr.length
   var temp = arr[n-1]
   
   // traversing over the array 
   for(var i = n-1; i >0; i--)    {
      arr[i] = arr[i-1];
   }
   arr[0] = temp;
   console.log("Array after right rotation is: " )
   console.log(arr)
}

// defining the array 
var arr = [1, 2, 3, 4, 5, 6];
console.log("Given array is: ")
console.log(arr)
Left_rotate(arr)

// redefining the array 
arr = [1, 2, 3, 4, 5, 6]
Right_rotate(arr)

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the array and the space complexity is O(1) as we are not using any extra space.

Another Approach

In the previous approach, we were maintaining the first or the last element and then assigning it. In this approach, we will swap the current element with the next element and move the first or last element to the required position. Let us see the code −

Example

// definging function to rotate the array in left direction 
function Left_rotate(arr){
   var n = arr.length
   var i = 0, j = n-1;
   var temp 
   while(i != j){
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      j--;
   }
   console.log("Array after left rotation is: " )
   console.log(arr)
}

// definging function to rotate the array in right direction 
function Right_rotate(arr){
   var n = arr.length
   var i = 0, j = n-1;
   var temp 
   while(i != j){
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;
   }
   console.log("Array after right rotation is: " )
   console.log(arr)
}

// defining the array 
var arr = [1, 2, 3, 4, 5, 6];
console.log("Given array is: ")
console.log(arr)
Left_rotate(arr)

// redefining the array 
arr = [1, 2, 3, 4, 5, 6]
Right_rotate(arr)

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the array and the space complexity is O(1) as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a JavaScript program to rotate an array in cyclic order by one. Cyclic rotation means shifting the value present at each index to their left or right by one and for one corner it takes the value present at the other corner. We have seen two approaches one works on the assigning property and another on swapping elements. Both work on the O(N) time complexity.

Updated on: 14-Apr-2023

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements