Javatpoint Logo
Javatpoint Logo

Reversal algorithm for Array rotation in C++

Problem statement: You are given an array, and your task is to rotate the array by one step for an integer number of times. Rotating an array means shifting the first element of the array to the last of the array so that the first element is present in the last position and every other element shifts towards the left.

For more clarity, we can understand this problem in this way. First of all, the element in the 0 index is taken, and the element in the first index shifts towards the left, which means it will occupy the index 0. Likewise, the element present in the second index will occupy the first index. In this way, all elements will shift left, and there will be the last index unoccupied, so this index is replaced with the first element in the array, which is initially taken out from the array. This entire operation should be repeated a given number of times.

Input format:

Array of integers int[] array

Number of rotations n


Print the array after n rotations

Example for illustration:

Array = {10, 20, 30, 40, 50, 60, 70, 80}

Num = 4;

Initial array: {10, 20, 30, 40, 50, 60, 70, 80}

After one rotation: {20, 30, 40, 50, 60, 70, 80, 10}

After the second rotation: {30, 40, 50, 60, 70, 80, 10, 20}

After the third rotation: {40, 50, 60, 70, 80, 10, 20, 30}

After the fourth rotation: {50, 60, 70, 80, 10, 20, 30, 40}


The solution for the above problem is given below:


Reversal algorithm for Array rotation in C++


This program contains two functions: one is the main function, and the other is the function that is used to reverse the array. This program is used for rotating the array. First, the program asks the user to enter the length of the array. After that, it asks the user to enter the elements to store in the array. After entering the elements, it asks for the number of rotations to do on the array. Now, we call the function named reverseFunction, which takes three parameters: that are integer array, the starting index, and the ending index. So, this reverseFunction is used to reverse the given integer array from the starting index to the ending index. The first time, the parameters are array, zero, one less than the number of rotations. Again, this reverseFunction is called with the parameters array, number of rotations, and one less than the size of the array. After that, the final call to the reverseFunction is made with the parameters integer array, zero, and one less than the size of the array. After this step, the array will be rotated, and then the resultant array will be printed.

The functionality of the reverseFunction. It takes an array and two indices as input. Using a while loop, it swaps elements at the first and last indices, moving towards the centre. This process effectively reverses the portion of the array between these indices. By swapping elements iteratively until the first and last indices meet, the function achieves a complete reversal. This method, based on simple swapping logic, demonstrates a fundamental technique widely used in computer programming for tasks like array manipulation and rotation. Its efficiency lies in its straightforward approach, making it both intuitive and practical for array reversal operations.


In conclusion, the C++ program efficiently rotates an array by a specified number of positions using a reversal algorithm. It provides an intuitive user interface, allowing seamless input and showcasing the rotated array accurately. The code demonstrates a clear understanding of array manipulation techniques, offering a practical solution.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA