Rotate array in C: solved exercise

Rotate array in C: solved exercise

If you searched for a solved rotate array exercise in C, here is the three-reversal trick: rotate an array k positions left or right in O(n) time and O(1) extra space, with no auxiliary buffer needed.

The idea is that rotating an array is equivalent to reversing three sub-sequences: the block being shifted, the remainder, and then the entire array.

Problem statement

Given the array {1, 2, 3, 4, 5, 6, 7}:

  1. Rotate it 3 positions to the left{4, 5, 6, 7, 1, 2, 3}.
  2. Rotate it 2 positions to the right{6, 7, 1, 2, 3, 4, 5}.

Use the three-reversal algorithm in both cases.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>

void reverse(int arr[], int lo, int hi) {
    while (lo < hi) {
        int tmp   = arr[lo];
        arr[lo++] = arr[hi];
        arr[hi--] = tmp;
    }
}

/* Left rotation by k positions */
void rotate_left(int arr[], int n, int k) {
    k %= n;
    if (k == 0) return;
    reverse(arr, 0, k - 1);
    reverse(arr, k, n - 1);
    reverse(arr, 0, n - 1);
}

/* Right rotation by k positions */
void rotate_right(int arr[], int n, int k) {
    k %= n;
    if (k == 0) return;
    reverse(arr, 0, n - 1);
    reverse(arr, 0, k - 1);
    reverse(arr, k, n - 1);
}

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

int main(void) {
    int a[] = {1, 2, 3, 4, 5, 6, 7};
    int b[] = {1, 2, 3, 4, 5, 6, 7};

    rotate_left(a, 7, 3);
    printf("Rotate left  3: "); print_arr(a, 7);

    rotate_right(b, 7, 2);
    printf("Rotate right 2: "); print_arr(b, 7);

    return 0;
}

Expected output

1
2
Rotate left  3: 4 5 6 7 1 2 3 
Rotate right 2: 6 7 1 2 3 4 5 

Common mistakes

  • Not reducing k with k %= n: if k >= n, computing k - 1 gives an out-of-bounds index.
  • Confusing left with right: rotating left by k is equivalent to rotating right by n - k.
  • Using an auxiliary buffer of size k: functionally correct but uses O(k) extra space; the three-reversal algorithm is O(1).
  • Getting the three ranges wrong: for a left rotation they must be [0, k-1], [k, n-1], and [0, n-1], in that order.

Practical use

Array rotation is used in circular buffers, FIFO queue implementations with arrays, signal processing (sliding windows), and string problems such as detecting rotation anagrams.

Guided practice and full book

If you want a complete path with progressive difficulty:

FAQ

Why does the three-reversal algorithm work?

Rotating left by k transforms [A|B] into [B|A]. Reversing A gives [A'|B], then reversing B gives [A'|B'], and finally reversing the whole array gives [B|A]. The three in-place reversals achieve the rotation without any copies.

How do you handle k larger than n?

Use k %= n. Rotating by n positions is the same as not rotating. With the modulo, k = 9 on a 7-element array is equivalent to k = 2.

Can the same algorithm rotate a string?

Yes. A C string is an array of char, so reverse and rotate_left work identically by replacing int with char and passing strlen(s) as n.