Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Array Rotation in C++

Last Updated on May 12, 2023 by Prepbytes

Array rotation is a common computer programming operation that involves shifting the elements of an array by a specified number of positions. This can be done in two ways:

• Rotate to the left.
• Rotate to the right.

## What is Array Rotation in C++?

Array rotation is the process of rearranging the elements of an array by shifting them by a specified number of positions.

## What is Left Rotation in C++?

The array elements are shifted to the left by a certain number of positions in left rotation, and the elements that "fall off" the left end of the array are added to the right end of the array. If we take an array [1, 2, 3, 4, 5] and rotate it by two positions, we get the array [3, 4, 5, 1, 2].

### Algorithm for Rotation of an Array by D Positions to the Left

Let us now look at how to rotate the array by d positions to the left. The steps are as follows:

1. Make a d-dimensional temporary array.
2. The first d elements of the original array are copied to the temporary array.
3. Shift the remaining elements of the original array by d positions to the left.
4. Add the temporary array elements to the right end of the original array.
5. The array has been rotated to the left by d.

### Pseudocode for Rotation of an Array by D Positions to the Left

``````function rotate_array(arr, n, d):
temp <- new array of size d
for i from 0 to d-1:
temp[i] <- arr[i]
for i from d to n-1:
arr[i-d] <- arr[i]
for i from 0 to d-1:
arr[n-d+i] <- temp[i]``````

### C++ Code for Array Rotation by D Positions to the Left.

```#include <iostream>
using namespace std;

void rotate_array_left(int arr[], int n, int d) {
d = d % n; // adjust d if it's greater than n
int temp[d];
for(int i = 0; i < d; i++) {
temp[i] = arr[i];
}
for(int i = 0; i < n - d; i++) {
arr[i] = arr[i + d];
}
for(int i = n - d; i < n; i++) {
arr[i] = temp[i - n + d];
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int d = 2;
rotate_array_left(arr, n, d);
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
```

Output :

``````Original Array : 1 2 3 4 5
New Array : 3 4 5 1 2``````

## What is Right Rotation in C++?

The elements of the array are shifted to the right by a specified number of positions in right rotation, and the elements that "fall off" the right end of the array are added to the left end of the array.
If we take an array [1, 2, 3, 4, 5] and rotate it by two positions, we get the array [4, 5, 1, 2, 3].

### Algorithm for Rotation of an Array by D Positions to the Right

Now consider how to rotate the array d positions to the right. The steps are as follows:

1. Determine the number of effective rotations k = d mod n, where n is the length of the array. This is due to the fact that d rotations are equivalent to k rotations if d is greater than or equal to n.
2. Reverse the first n-k elements of the array.
3. Reverse the last k array elements.
4. Turn the entire array around.

### Pseudocode for Rotation of an Array by D Positions to the Right

``````function rotate_array_right(arr, n, d):
d <- d % n  // Adjust d if it's greater than n
reverse_array(arr, n - d, n - 1)
reverse_array(arr, 0, n - d - 1)
reverse_array(arr, 0, n - 1)

function reverse_array(arr, start, end):
while start < end:
temp <- arr[start]
arr[start] <- arr[end]
arr[end] <- temp
start <- start + 1
end <- end - 1``````

### C++ Code for Array Rotation by D Positions to the Right.

```#include <iostream>
using namespace std;

void rotate_array_right(int arr[], int n, int d) {
d = d % n; // adjust d if it's greater than n
int temp[d];
for(int i = 0; i < d; i++) {
temp[i] = arr[n - d + i];
}
for(int i = n - 1; i >= d; i--) {
arr[i] = arr[i - d];
}
for(int i = 0; i < d; i++) {
arr[i] = temp[i];
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int d = 2;
rotate_array_right(arr, n, d);
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
```

Output :

``````Original Array : 1 2 3 4 5
New Array : 4 5 1 2 3 ``````

Conclusion
The concept of array rotation in computer programming is explained in this article, which involves shifting the elements of an array by a specified number of positions. It goes over two array rotation methods: left and right rotation. The article includes algorithms and pseudocode for both types of rotation, as well as C++ code examples for each.

Q1. How do I rotate an array left in C++?
Ans. The array can be left rotated by shifting its elements to a position before them, which is done by looping through the array and performing the operation arr[j] = arr[j+1]. The first element of the array will be added to the last element of the rotated array.

Q2. What's a rotated array?
Ans. Array rotation is simply the movement of array elements to the left or right of the array by specified positions.

Q3. What is the left and right rotation of an array in C++?
Ans. If all of the selected elements were moved to the right by one position, the array is said to be right rotated.

Q4. Which algorithm is used to rotate an array?
Ans. The block swap algorithm is used to perfectly rotate arrays.