0
0
CProgramBeginner · 2 min read

C Program to Rotate Array Elements

A C program to rotate an array by d positions uses a loop to shift elements left and moves the first d elements to the end; for example, use a function that stores the first d elements, shifts the rest left, then appends the stored elements at the end.
📋

Examples

InputArray: [1, 2, 3, 4, 5], Rotate by: 2
OutputRotated array: 3 4 5 1 2
InputArray: [10, 20, 30, 40, 50, 60], Rotate by: 4
OutputRotated array: 50 60 10 20 30 40
InputArray: [7, 8, 9], Rotate by: 0
OutputRotated array: 7 8 9
🧠

How to Think About It

To rotate an array by d positions, think of taking the first d elements out temporarily, then moving the remaining elements forward to fill the gap, and finally placing the removed elements at the end. This way, the array elements shift left by d places, wrapping around to the start.
📐

Algorithm

1
Get the array and the number of positions <code>d</code> to rotate.
2
Store the first <code>d</code> elements in a temporary array.
3
Shift the remaining elements of the array to the left by <code>d</code> positions.
4
Copy the stored elements from the temporary array to the end of the original array.
5
Print the rotated array.
💻

Code

c
#include <stdio.h>

void rotateArray(int arr[], int n, int d) {
    d = d % n; // Handle cases where d >= n
    int temp[d];
    for (int i = 0; i < d; i++) {
        temp[i] = arr[i];
    }
    for (int i = d; i < n; i++) {
        arr[i - d] = arr[i];
    }
    for (int i = 0; i < d; i++) {
        arr[n - d + i] = temp[i];
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int d = 2;
    rotateArray(arr, n, d);
    printf("Rotated array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
Output
Rotated array: 3 4 5 1 2
🔍

Dry Run

Let's trace rotating the array [1, 2, 3, 4, 5] by 2 positions through the code.

1

Store first d elements

temp = [1, 2]

2

Shift remaining elements left

arr after shift = [3, 4, 5, 4, 5]

3

Copy temp elements to end

arr final = [3, 4, 5, 1, 2]

IndexValue after rotation step
03
14
25
31
42
💡

Why This Works

Step 1: Store first d elements

We save the first d elements in a temporary array so we don't lose them when shifting.

Step 2: Shift elements left

We move each element from position d onward to the left by d places.

Step 3: Append stored elements

We place the saved elements at the end of the array to complete the rotation.

🔄

Alternative Approaches

Rotate one by one
c
#include <stdio.h>

void rotateByOne(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 rotateArray(int arr[], int n, int d) {
    d = d % n; // Handle cases where d >= n
    for (int i = 0; i < d; i++) {
        rotateByOne(arr, n);
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int d = 2;
    rotateArray(arr, n, d);
    printf("Rotated array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
This method rotates the array by one position repeatedly, which is simple but less efficient for large rotations.
Using reversal algorithm
c
#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 rotateArray(int arr[], int n, int d) {
    d = d % n;
    reverse(arr, 0, d - 1);
    reverse(arr, d, n - 1);
    reverse(arr, 0, n - 1);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int d = 2;
    rotateArray(arr, n, d);
    printf("Rotated array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
This method uses three reversals to rotate the array efficiently in-place without extra space.

Complexity: O(n) time, O(d) space

Time Complexity

The main method loops through the array elements a few times, so it runs in linear time proportional to the array size.

Space Complexity

It uses extra space to store the first d elements temporarily, so space complexity is O(d).

Which Approach is Fastest?

The reversal algorithm is fastest and uses O(1) extra space, while the temporary array method uses O(d) space and the one-by-one rotation is slower with O(n*d) time.

ApproachTimeSpaceBest For
Temporary arrayO(n)O(d)Simple and easy to understand
Rotate one by oneO(n*d)O(1)Small arrays or small rotations
Reversal algorithmO(n)O(1)Efficient in-place rotation
💡
Use the reversal algorithm for efficient in-place rotation without extra memory.
⚠️
Forgetting to handle cases where rotation count is greater than array size causes incorrect results.