C++ Program to Rotate an Array

Array Rotation

C++ program for array rotation has been shown here. An array rotation is described as the rotation of array elements to either left or right by k positions.


Example 1: Left rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the left.

=>A[] = {20, 30, 40, 50, 60, 10}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {30, 40, 50, 60, 10, 20}

So, array A[] after left rotation by 2 positions: {30, 40, 50, 60, 10, 20}



Example 2: Right rotation

Given array A[] = {10, 20, 30, 40, 50, 60}, k = 2

Rotate elements of A[] by 1 position to the right.

=>A[] = {60, 10, 20, 30, 40, 50}

Again rotate elements of A[] by 1 position to the left.

=>A[] = {50, 60, 10, 20, 30, 40}

So, array A[] after right rotation by 2 positions: {50, 60, 10, 20, 30, 40}






1. C++ program & output to Rotate an Array

Code has been copied
/*****************************
       alphabetacoder.com
C++ program to rotate an array
******************************/

#include<iostream>

using namespace std;

int main() {
    // declare variables
    int i, j, k, s, d, e;
    int arr[20] = {0};

    // take input
    cout << "Enter the size of array: ";
    cin >> s;

    // take input
    cout << "Enter the elements of array: ";
    for (i = 0; i < s; i++) {
        cin >> arr[i];
    }

    // take input of the direction of rotation
    cout << "Enter the direction of rotation: " << endl;
    cout << "1. Left rotation" << endl;
    cout << "2. Right rotation" << endl;
    cout << "Your choice >> ";
    cin >> d;

    switch (d) 
    {
        case 1:
            // take input of the number of position of rotation
            cout << "Enter the number of position to be left shifted: ";
            cin >> k;
    
            // left shift the array upto k position
            for (i = 0; i < k; i++) {
                // get the first elements
                e = arr[0];
                for (j = 0; j < s - 1; j++) {
                    arr[j] = arr[j + 1];
                }
                arr[s - 1] = e;
            }
            // display the array
            cout << "Output array: ";
            for (i = 0; i < s; i++) {
                cout << arr[i] << " ";
            }
            break;
        case 2:
            // take input of the number of position of rotation
            cout << "Enter the number of position to be right shifted: ";
            cin >> k;
    
            // right shift the array upto k position
            for (i = 0; i < k; i++) {
                // get the first elements
                e = arr[s - 1];
                for (j = s - 1; j > 0; j--) {
                    arr[j] = arr[j - 1];
                }
                arr[0] = e;
            }
            // display the array
            cout << "Output array: ";
            for (i = 0; i < s; i++) {
                cout << arr[i] << " ";
            }
            break;
        default:
            cout << "Wrong choice!";
            break;
    }
}

Output


Case 1:

Enter the size of array: 8

Enter the elements of array: 1 2 3 4 5 6 7 8

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 1

Enter the number of position to be left shifted: 4

Output array: 5 6 7 8 1 2 3 4


Case 2:

Enter the size of array: 8

Enter the elements of array: 1 2 3 4 5 6 7 8

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 2

Enter the number of position to be right shifted: 5

Output array: 4 5 6 7 8 1 2 3



2. Time complexity to Rotate an Array


Time Complexity: O(n * k)

Here, $n$ elements in an array are to be rotated by $k$ positions to either left or right.