Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Reversal algorithm for array rotation
An array is a linear data structure used to store different types of objects. Given an array of size n and an integer k, we need to rotate the array by k elements to the left and return the rotated array.
Rotation means shifting all elements to the left by k positions. Elements that move beyond the first position wrap around to the end of the array.
Note ? During rotation, when elements shift left, those at the beginning move to the end, creating a circular shift pattern.
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 ? If k is greater than n, we use k = k % n to handle larger rotation values efficiently.
Reversal Algorithm Approach
The reversal algorithm uses three reverse operations to achieve array rotation:
Divide the array into two parts: first part from index 0 to k-1, second part from index k to n-1
Reverse the first part (elements 0 to k-1)
Reverse the second part (elements k to n-1)
Reverse the entire array to get the final rotated result
Algorithm Steps
For array [1, 2, 3, 4, 5] with k = 2:
Original: [1, 2, 3, 4, 5] Step 1: Reverse first k elements ? [2, 1, 3, 4, 5] Step 2: Reverse remaining elements ? [2, 1, 5, 4, 3] Step 3: Reverse entire array ? [3, 4, 5, 1, 2]
Implementation
// Function to reverse array elements between Left and Right indices
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 perform left rotation using reversal algorithm
function left_rotate(arr, k) {
var len = arr.length;
if (k == 0) return arr;
// Handle cases where k > array length
k = k % len;
// Step 1: Reverse first k elements
arr = revArray(arr, 0, k-1);
// Step 2: Reverse remaining elements
arr = revArray(arr, k, len-1);
// Step 3: Reverse entire array
arr = revArray(arr, 0, len-1);
return arr;
}
// Function to print array elements
function print(arr) {
var len = arr.length;
var temp = "";
for (var i = 0; i < len; i++) {
temp += arr[i] + " ";
}
console.log(temp.trim());
}
// Test the algorithm
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);
The given array is: 1 5 7 8 2 2 6 9 1 5 2 The Array after 3 rotations is: 8 2 2 6 9 1 5 2 1 5 7
How It Works
The reversal algorithm works by strategically reversing parts of the array:
- Reversing the first k elements puts them in reverse order
- Reversing the remaining elements puts them in reverse order
- Reversing the entire array restores the correct order while achieving the rotation
Time and Space Complexity
| Complexity | Value | Explanation |
|---|---|---|
| Time | O(n) | Three passes through the array |
| Space | O(1) | In-place rotation using only temporary variables |
Conclusion
The reversal algorithm provides an efficient O(n) time and O(1) space solution for array rotation. It uses three strategic reverse operations to achieve left rotation without requiring additional memory for copying elements.
