Cyclic Rotation of Array in C – Left and Right Rotation with Example

A cyclic rotation of an array in C shifts every element one position to the left or right, with the element that falls off one end wrapping around to the other. A left rotation moves the first element to the last position; a right rotation moves the last element to the first position. This operation appears in scheduling algorithms, circular buffer implementations, and data transformation pipelines where elements cycle through positions.

How It Works — Step by Step

Given the array [10, 20, 30, 40, 50]:

Left rotation — first element wraps to the end:

  1. Save arr[0] = 10 (it will be overwritten).
  2. Shift every element one position left: arr[0]=20, arr[1]=30, arr[2]=40, arr[3]=50.
  3. Place the saved value at the last position: arr[4]=10.
  4. Result: [20, 30, 40, 50, 10]

Right rotation — last element wraps to the front:

  1. Save arr[4] = 50.
  2. Shift every element one position right: arr[4]=40, arr[3]=30, arr[2]=20, arr[1]=10.
  3. Place the saved value at position 0: arr[0]=50.
  4. Result: [50, 10, 20, 30, 40]

C Program for Cyclic Rotation of an Array

/* Cyclic left and right rotation of an array in C
 * Compile: gcc -ansi -Wall -Wextra cyclic.c -o cyclic */
#include <stdio.h>

#define MAX 100

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

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

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

int main(void)
{
    int arr[MAX], left[MAX], right[MAX], n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    rotate_left(arr, n, left);
    rotate_right(arr, n, right);

    printf("Original:           ");
    print_array(arr, n);

    printf("After left rotate:  ");
    print_array(left, n);

    printf("After right rotate: ");
    print_array(right, n);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra cyclic.c -o cyclic
./cyclic

Sample Input and Output

Test 1:

Enter number of elements: 5
Enter 5 elements:
10 20 30 40 50
Original:           10 20 30 40 50
After left rotate:  20 30 40 50 10
After right rotate: 50 10 20 30 40

Test 2:

Enter number of elements: 4
Enter 4 elements:
1 2 3 4
Original:           1 2 3 4
After left rotate:  2 3 4 1
After right rotate: 4 1 2 3

Code Explanation

  • copy[] arrays — both rotate_left() and rotate_right() write into a separate output array, leaving the original untouched. This lets both rotations be computed from the same input in one pass.
  • rotate_left logic — assigns arr[0] to copy[n-1] first to avoid losing it, then copies arr[1..n-1] into copy[0..n-2].
  • rotate_right logic — assigns arr[n-1] to copy[0] first, then copies arr[0..n-2] into copy[1..n-1].
  • #define MAX 100 — a named constant avoids VLAs (variable-length arrays), which are optional in C11 and absent in C89/C90. The program compiles cleanly under -ansi.

Time and Space Complexity

Operation Time Space
Single rotation (left or right) O(n) O(n) — copy array
In-place single rotation O(n) O(1) — one saved element
k rotations (naive) O(k × n) O(1)

What This Program Teaches

  • Array wrap-around with a saved element — saving the element that would be overwritten before shifting is the key pattern in all rotation and reverse algorithms.
  • Read from one array, write to another — avoids aliasing bugs when computing two results from the same source in a single pass.
  • Arrays as function parameters — arrays in C decay to pointers when passed to functions; int arr[] and int *arr are equivalent in a parameter list.
  • Named constants with #define — using MAX instead of a magic number makes the bound easy to change and self-documenting.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>