- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# JavaScript Program for Reversal algorithm for array rotation

An array is a linear data structure used to store the different types of objects and we are given an array of size n and an integer k (where k is the number by which we will rotate an array). We will rotate the array by k elements and then return the rotated array.

Rotation means we have to traverse the array from the given kth element and shift all the integers by one and we have to shift each element by 1 till the kth element came to position ‘0’ or we can say at index number ‘0’.

**Note** − In the rotation when we shift all elements by one then whenever it comes to the last index its value shifts to the index 0 and index 0’s value shifts to the index 1 and so on.

Let us see an example −

Input: n = 5 array = [1, 2, 3, 4, 5] k = 2; Output: Rotated array = [3, 4, 5, 1, 2]

**Note** − In the above example, it is assumed that k is less than or equal to n. By performing k = k% n, we can readily change the answers to handle bigger k numbers.

## Approach

To solve this problem, we are going to follow these steps

First, we will divide the array into two parts. The first part is from index zero to index k-1 and the second part is from index k to index n-1.

f = array[0…k-1] and s = array[k..n-1]

We have a function name reverse in which we have to pass the above-mentioned fs array to get the ‘sf’ array.

The main part of our algorithm is,

reverse array ‘f’ to get ‘rfs’ (rf means a reverse array of f),

reversed array ‘s’ to get ‘rfrs’ (rs means reversed array of s),

reverse array ‘rfrs’ to get ‘sf’.

In the end, we will print the array rotated by k elements.

Example

n = 5 array = [1, 2, 3, 4 ,5] k = 2 f = [1, 2] and s = [3, 4, 5] Reverse f, to get ‘rfs’ = [2, 1, 3, 4, 5] Reverse s to get ‘rfrs’ = [2, 1, 5, 4, 3] Reverse ‘rfrs’ to get ‘sf’ = [3, 4, 5, 1, 2]

### Example

Let’s see the proper code to implement the above-given steps for better understanding

// 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 getting the mod and calling another function function left_rotate(arr, k) { var len = arr.length if (k == 0) return arr; // if rotations array k = k % len; arr = revArray(arr, 0, k-1); arr = revArray(arr, 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, 5, 7, 8, 2, 2, 6, 9, 1, 5, 2]; var number_of_rotations = 3 console.log("The given array is: "); print(arr); console.log("The Array after " + number_of_rotations +" rotations is: ") arr = left_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 number of the size of the array as we have just traversed over the array only once.

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

## Conclusion

In this tutorial, we have implemented a JavaScript program to rotate an array by k elements using a reversal algorithm. We have traversed over the array of size n and reversed the array in the reverse function and print the rotated array. The time complexity of the above code is O(N) and the space complexity of the above code is O(1).