C++ Program to Rotate Array Right by One Position



Rotating an array means shifting its elements in a specific direction while maintaining their relative order. In this article, we will rotate an array right by one position using C++. For example, if the array is {4, 8, 15, 16, 23}, rotating it right once will result in {23, 4, 8, 15, 16}. We are given an array and need to shift all elements one step to the right, with the last element moving to the first position.

Example 1

  • Input:
    array = {10, 20, 30, 40, 50}
  • Output:
    array = {50, 10, 20, 30, 40}

Explanation:

The last element 50 moves to the first position, and the rest shifts to the right.

Example 2

  • Input:
    array = {3, 7, 1, 9, 5}
  • Output:
    array = {5, 3, 7, 1, 9}

Explanation:

The last element 5 moves to the front, shifting the rest to the right.

Using the Direct Swapping Approach

This is a simple and direct approach where we store the last element of the array in a temporary variable. In this approach, we first shift all elements to one position to the right and place the last element in the first position.

Steps for Implementation

  • Store the last element in a temporary variable.
  • Shift all elements to the right by one position.
  • Place the last element in the first position.
  • Print the rotated array.

Implementation Code

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

void rotateRight(int arr[], int n) {
  int temp = arr[n - 1];
  for (int i = n - 1; i > 0; i--) {
    arr[i] = arr[i - 1];
  }
  arr[0] = temp;
}

int main() {
  int arr[] = {1, 2, 3, 4, 5};
  int n = sizeof(arr) / sizeof(arr[0]);

  rotateRight(arr, n);

  cout << "Rotated Array: ";
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  return 0;
}

Output:

Rotated Array: 5 1 2 3 4

Time Complexity: O(N)
Space Complexity: O(1)

Rotate Array Right Using Temporary Variable

We can use a temporary variable to store the last element, then shift all elements one position to the right, and finally place the last element in the first position.

Steps for Implementation

  • First, store the last element of the array in a temporary variable.
  • Shift all elements from index n-2 to 0 one step to the right.
  • Place the last element at the first index.
  • Print or return the modified array.

Implementation Code

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

void rotateRight(int arr[], int n) {
  if (n <= 1) return; // No rotation needed for small arrays
  int last = arr[n - 1];
  for (int i = n - 1; i > 0; i--) {
    arr[i] = arr[i - 1];
  }
  arr[0] = last;
}

int main() {
  int array[] = {10, 20, 30, 40, 50};
  int n = sizeof(array) / sizeof(array[0]);
  rotateRight(array, n);

  cout << "Array after right rotation: ";
  for (int i = 0; i < n; i++) {
    cout << array[i] << " ";
  }
  return 0;
}

Output

Array after right rotation: 50 10 20 30 40

Time Complexity: O(n)
Space Complexity: O(1)

Using STL rotate() Function

In this approach, we use the STL built-in function std::rotate in the <algorithm> library, which can be used to rotate the array efficiently.

Steps for Implementation

  • Use std::rotate() to rotate the array.
  • Pass arr, arr + (n-1), arr + n as arguments.
  • Print the rotated array.

Implementation Code

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

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    rotate(arr, arr + (n - 1), arr + n);
    
    cout << "Rotated Array: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

Output

Rotated Array: 5 1 2 3 4

Time Complexity: O(N)
Space Complexity: O(1)

Using STL Deque

This is the most efficient approach for rotating an array right by one position. In this approach, we use deque from C++ STL which allows the right rotation of the array easily using push_front() and pop_back().

Steps for Implementation

  • Use deque for efficient insertion and deletion.
  • Move the last element to the front.
  • Print the modified rotated array.

Implementation Code

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

int main() {
    deque <int> arr = {1, 2, 3, 4, 5};
    arr.push_front(arr.back());
    arr.pop_back();
    
    cout << "Rotated Array: ";
    for (int num : arr) {
        cout << num << " ";
    }
    return 0;
}

Output:

Rotated Array: 5 1 2 3 4

Time Complexity: O(1)
Space Complexity: O(N)

Conclusion

We have seen multiple ways to rotate an array to the right by one position in C++. The most efficient approach is using std::rotate or deque, while the swapping method is a simple and effective solution without extra space. We can choose any approach based on the conditions and requirements of our application.

Updated on: 2025-03-18T19:12:42+05:30

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements