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<stdio.h>

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

    // take input
    printf("Enter the size of array: ");
    scanf("%d", & s);

    // take input
    printf("Enter the elements of array: ");
    for (i = 0; i < s; i++) {
        scanf("%d", & arr[i]);
    }

    // take input of the direction of rotation
    printf("Enter the direction of rotation: \n");
    printf("1. Left rotation\n");
    printf("2. Right rotation\n");
    printf("Your choice >> ");
    scanf("%d", & d);

    switch (d) {
        case 1:
            // take input of the number of position of rotation
            printf("Enter the number of position to be left shifted: ");
            scanf("%d", & 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
            printf("Output array: ");
            for (i = 0; i < s; i++) {
                printf("%d ", arr[i]);
            }
            break;
        case 2:
            // take input of the number of position of rotation
            printf("Enter the number of position to be right shifted: ");
            scanf("%d", & 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
            printf("Output array: ");
            for (i = 0; i < s; i++) {
                printf("%d ", arr[i]);
            }
            break;
        default:
            printf("Wrong choice!");
            break;
    }
}

Output


Case 1:

Enter the size of array: 5

Enter the elements of array: 0 5 10 15 20

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 1

Enter the number of position to be left shifted: 3

Output array:

15 20 0 5 10


Case 2:

Enter the size of array: 5

Enter the elements of array: 0 5 10 15 20

Enter the direction of rotation:

1. Left rotation

2. Right rotation

Your choice >> 2

Enter the number of position to be right shifted: 3

Output array:

10 15 20 0 5



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.