C Program for array rotation?

Array rotation is a fundamental operation where elements of an array are shifted to the left or right by a specified number of positions. In left rotation, elements move left and the leftmost elements wrap around to the end.

Left Rotation by 3 positions Original: 1 2 3 4 5 6 Rotated: 4 5 6 1 2 3 First 3 elements move to end

Syntax

void leftRotate(int arr[], int n, int rotations);

Example: Left Rotation by N Positions

This program demonstrates left rotation by shifting elements one position at a time −

#include <stdio.h>

void leftRotate(int arr[], int n, int rotations) {
    int i, j, temp;
    
    for (i = 0; i < rotations; i++) {
        temp = arr[0];  // Store first element
        
        // Shift all elements one position left
        for (j = 0; j < n - 1; j++) {
            arr[j] = arr[j + 1];
        }
        
        arr[n - 1] = temp;  // Place first element at end
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n = 10;
    int rotations = 3;
    int i;
    
    printf("Original array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("
"); leftRotate(arr, n, rotations); printf("After left rotation by %d: ", rotations); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("
"); return 0; }
Original array: 1 2 3 4 5 6 7 8 9 10 
After left rotation by 3: 4 5 6 7 8 9 10 1 2 3 

Optimized Approach: Reversal Algorithm

A more efficient approach uses array reversal technique −

#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 leftRotateOptimized(int arr[], int n, int rotations) {
    rotations = rotations % n;  // Handle rotations greater than array size
    
    // Reverse first 'rotations' elements
    reverse(arr, 0, rotations - 1);
    
    // Reverse remaining elements
    reverse(arr, rotations, n - 1);
    
    // Reverse entire array
    reverse(arr, 0, n - 1);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n = 10;
    int rotations = 3;
    int i;
    
    printf("Original array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("
"); leftRotateOptimized(arr, n, rotations); printf("After optimized left rotation by %d: ", rotations); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("
"); return 0; }
Original array: 1 2 3 4 5 6 7 8 9 10 
After optimized left rotation by 3: 4 5 6 7 8 9 10 1 2 3 

Comparison

Method Time Complexity Space Complexity Rotations
One by One O(n × rotations) O(1) Multiple shifts
Reversal Algorithm O(n) O(1) Three reversals

Conclusion

Array rotation can be implemented using simple shifting or the efficient reversal algorithm. The reversal method is optimal with O(n) time complexity and works well for large rotations.

Updated on: 2026-03-15T11:42:28+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements