Rotation of Elements of Array - Left and Right in C

Understanding Array Rotation

Array rotation involves shifting elements either to the left or right by a specified number of positions.

We will explore three different methods to perform left and right rotation in C.

Method 1: Using Temporary Array

This method uses a temporary array to store rotated elements.

#include <stdio.h>

void leftRotate(int arr[], int n, int d) {
    int temp[d];
    for (int i = 0; i < d; i++)
        temp[i] = arr[i];
    for (int i = 0; i < n - d; i++)
        arr[i] = arr[i + d];
    for (int i = 0; i < d; i++)
        arr[n - d + i] = temp[i];
}

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

void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5;
    leftRotate(arr, n, 2);
    printf("Array after left rotation: ");
    printArray(arr, n);
    rightRotate(arr, n, 2);
    printf("Array after right rotation: ");
    printArray(arr, n);
    return 0;
}            
Output:
Array after left rotation: 3 4 5 1 2
Array after right rotation: 1 2 3 4 5

Method 2: Using Reversal Algorithm

This method reverses subarrays to perform rotation.

#include <stdio.h>

void reverse(int arr[], int start, int end) {
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

void leftRotate(int arr[], int n, int d) {
    reverse(arr, 0, d - 1);
    reverse(arr, d, n - 1);
    reverse(arr, 0, n - 1);
}

void rightRotate(int arr[], int n, int d) {
    reverse(arr, 0, n - 1);
    reverse(arr, 0, d - 1);
    reverse(arr, d, n - 1);
}

void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5;
    leftRotate(arr, n, 2);
    printf("Array after left rotation: ");
    printArray(arr, n);
    rightRotate(arr, n, 2);
    printf("Array after right rotation: ");
    printArray(arr, n);
    return 0;
}            
Output:
Array after left rotation: 3 4 5 1 2
Array after right rotation: 1 2 3 4 5

Method 3: Rotating One by One

This method rotates the array by shifting elements one by one.

#include <stdio.h>

void leftRotateOne(int arr[], int n) {
    int temp = arr[0];
    for (int i = 0; i < n - 1; i++)
        arr[i] = arr[i + 1];
    arr[n - 1] = temp;
}

void rightRotateOne(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;
}

void leftRotate(int arr[], int n, int d) {
    for (int i = 0; i < d; i++)
        leftRotateOne(arr, n);
}

void rightRotate(int arr[], int n, int d) {
    for (int i = 0; i < d; i++)
        rightRotateOne(arr, n);
}

void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5;
    leftRotate(arr, n, 2);
    printf("Array after left rotation: ");
    printArray(arr, n);
    rightRotate(arr, n, 2);
    printf("Array after right rotation: ");
    printArray(arr, n);
    return 0;
}            
Output:
Array after left rotation: 3 4 5 1 2
Array after right rotation: 1 2 3 4 5
Top 100 Codes By Learn-for-free
Start Preparing Arraysform here👇

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Find Largest Element in an Array: C C++ Java Python

Find Smallest Element in an Array: C C++ Java Python

Find the Smallest and Largest Element in an Array: C C++ Java Python

Find Second Smallest Element in an Array: C C++ Java Python

Calculate the Sum of Elements in an Array: C C++ Java Python

Reverse an Array: C C++ Java Python

Sort First Half in Ascending Order and Second Half in Descending: C C++ Java Python

Finding the Frequency of Elements in an Array: C C++ Java Python

Counting the Number of Even and Odd Elements in an Array: C C++ Java Python

Finding Maximum Product Sub-array in a Given Array: C C++ Java Python

Finding Arrays are Disjoint or Not: C C++ Java Python

Finding Equilibrium Index of an Array: C C++ Java Python

Rotation of Elements of Array - Left and Right: C C++ Java Python

Balanced Parenthesis Problem: C C++ Java Python