Reversal Algorithm for Array Rotation using C++


In the given problem, we are given an array, and we are required to rotate the array by d elements using a reversal algorithm, for example −

Input : arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Output : arr[] = [3, 4, 5, 6, 7, 1, 2]
Explanation : As you can see we have to rotate this array by d = 2 but our main task is to achieve this by using a reversal technique.

We make some calculations for the rotation of the array by reversal technique, and we conclude that −

  • First, we reverse the first d elements of the array.
  • Second, we reverse the remaining elements.
  • Third, we reverse the whole array.

And by applying these three steps, we can get our rotated array.

Approach to find The Solution

In this problem, firstly, we are going to make a function for reversing the elements; now we follow the steps given above

Example

#include <bits/stdc++.h>
using namespace std;

void reverseArray(int arr[], int start, int end) { // our reversal algorithm
   while (start < end) { // if start becomes equal to end we break the loop
      int temp = arr[start];
      arr[start] = arr[end];
      arr[end] = temp;
      start++;
      end--;
   }
   return ;
}
void Rotate(int arr[], int d, int n) { // rotation function
   if (d == 0) // no rotation required
      return;
   d = d % n; // when d becomes equal to n so our array comes to its original form
   reverseArray(arr, 0, d - 1); // reversing first d elements
   reverseArray(arr, d, n - 1); // reversing the remaining elements
   reverseArray(arr, 0, n - 1); // reversing the whole array

   return ;
}
int main() {
   int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; // given array
   int n = sizeof(arr) / sizeof(arr[0]); // size of our array
   int d = 2;
   Rotate(arr, d, n);
   for(int i = 0; i < n; i++) // printing the array
      cout << arr[i] << " ";
   cout << "\n";
   return 0;
}

Output

3 4 5 6 7 1 2

Explanation of the above code

In the above approach, we first create our reversal technique, which will take three parameters, i.e., array, starting index, and ending index, and reverse our array from start to end now. As we developed our algorithm earlier, we are going to apply that algorithm using this function. We firstly reverse the first d elements. Now secondly, we reverse the remaining elements, and at last, we reverse the whole array. As a result, our array is rotated by d. In the rotate function, we are making d = d % n. This is because if we rotate the first n elements of an array, the answer we get will be the same as before, so that’s why we make a mod of d with n.

Conclusion

In this article, we solve a problem to apply the reversal algorithm for array rotation. We also learned the C++ program for this problem and the complete approach ( Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.

Updated on: 29-Nov-2021

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements