# JavaScript Program for Reversal algorithm for right rotation of an array

Right rotation of an array means to rotate the elements of the array to their right side by a given number of times and for the number which are present at the edge they will move to the first index in their right rotation by assuming an array in a cycle form. We will implement a proper code to implement the algorithm with an explanation.

Example

Let us assume we have given array as
[1, 2, 3, 4, 5, 6, 7, 8, 9]
The number of right rotations of the array is 3
Output:
7 8 9 1 2 3 4 5 6


Explanation

In the first rotation, all the elements move to their right side and the last element will come to the zeroth index.

9 1 2 3 4 5 6 7 8

In the second rotation, again all the elements move to their next index, and the last element comes to the zeroth index.

8 9 1 2 3 4 5 6 7

Finally, after the third rotation array, looks like this:

7 8 9 1 2 3 4 5 6

## Approach

We have seen the example and now let us move to the approach to implement the code, but before moving to the exact approach let us see an observation.

For the example, we can observe that the last k elements have come to the first k places and the first total elements minus k elements have moved to the last position, which means if we reverse the last k elements differently and reverse remaining elements differently and then reverse the whole array then we will get the resultant array. So, the steps to implement the code are:

• First, we will create an array to reverse the elements of the array passed to it as the parameter and provided by the range to reverse.

• We will simply traverse over the given array from the provided first and the last position and swap the values of current indexes and move first pointer to one forward and another to one backward.

• We will create a function to take the array and number of rotations as the parameter and first we will get the mod of number of rotations with the size of the array.

• After that, we will call to the reverse function three times by passing the array and the range will be

• Last k elements

• First total minus k elements

• Whole array.

• By this way the array will be rotated given number of times in the right direction and we will print it at the last.

### Example

// Function to reverse array elements
function revArray(arr, Left, Right) {
while (Left < Right) {
var temp = arr[Left];
arr[Left] = arr[Right];
arr[Right] = temp;
Right--;
Left++;
}
return arr;
}
// function to get the mod and call another function
function right_rotate(arr, k) {
var len = arr.length
if (k == 0) return arr;
// if rotations array
k = k % len;
arr = revArray(arr, 0, (len-k-1));
arr = revArray(arr, len-k, len-1);
arr = revArray(arr, 0, len-1);
return arr;
}
// Function to print elements of array
function print(arr){
var len = arr.length
// traversing over the array
var temp = "";
for (var i = 0; i < len; i++) {
temp += arr[i];
temp += " ";
}
console.log(temp)
}
// defining array
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var number_of_rotations = 3
console.log("The given array is: ");
print(arr);
console.log("The Array after " +number_of_rotations+ " rotations is: ")
arr = right_rotate(arr, number_of_rotations);
print(arr);


### Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the array. We are traversing over the array two times which leads to linear time complexity.

The space complexity of the above code is O(1), as we are not using any extra space.

## Conclusion

In this tutorial, we have implemented a JavaScript program to rotate the elements of a given array in the right side given number of times. We have implemented the reversal algorithm in which we have first reversed the first length minus given number of elements and then we have reversed the remaining elements and at all the elements. The time complexity of the above code is linear and the space complexity is constant.

Updated on: 12-Apr-2023

63 Views 