Bird
0
0
DSA Cprogramming

Array Rotation Techniques in DSA C

Choose your learning style9 modes available
Mental Model
Rotating an array means moving its elements so that some elements shift to the front or back, like spinning a circle of items.
Analogy: Imagine a merry-go-round with horses numbered 1 to 5. Rotating the array is like spinning the merry-go-round so the horse at position 3 moves to the front.
Array: [1] -> [2] -> [3] -> [4] -> [5] -> null
Rotation by 2 to the left means the array becomes:
[3] -> [4] -> [5] -> [1] -> [2] -> null
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], rotate left by 2
Goal: Shift elements left by 2 positions so the first two elements move to the end
Step 1: Store first 2 elements temporarily
[1, 2, 3, 4, 5] (temp = [1, 2])
Why: We need to keep the first 2 elements safe before shifting
Step 2: Shift elements from index 2 to front
[3, 4, 5, 4, 5]
Why: Move elements left to fill the gap left by removed elements
Step 3: Place stored elements at the end
[3, 4, 5, 1, 2]
Why: Complete rotation by putting saved elements at the back
Result:
[3] -> [4] -> [5] -> [1] -> [2] -> null
Annotated Code
DSA C
#include <stdio.h>

void rotateLeft(int arr[], int n, int d) {
    if (d == 0 || d == n) return; // no rotation needed

    int temp[d];
    // Store first d elements
    for (int i = 0; i < d; i++) {
        temp[i] = arr[i];
    }

    // Shift remaining elements to the left
    for (int i = d; i < n; i++) {
        arr[i - d] = arr[i];
    }

    // Place stored elements at the end
    for (int i = 0; i < d; i++) {
        arr[n - d + i] = temp[i];
    }
}

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

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int d = 2;

    rotateLeft(arr, n, d);
    printArray(arr, n);

    return 0;
}
if (d == 0 || d == n) return; // no rotation needed
skip rotation if d is zero or equal to array size
for (int i = 0; i < d; i++) { temp[i] = arr[i]; }
save first d elements temporarily
for (int i = d; i < n; i++) { arr[i - d] = arr[i]; }
shift elements left by d positions
for (int i = 0; i < d; i++) { arr[n - d + i] = temp[i]; }
put saved elements at the end
OutputSuccess
3 -> 4 -> 5 -> 1 -> 2 -> null
Complexity Analysis
Time: O(n) because we move each element at most twice: once when shifting and once when copying temp
Space: O(d) because we use extra space to store d elements temporarily
vs Alternative: Compared to rotating by moving one element at a time d times (O(n*d)), this method is more efficient for large arrays and rotations
Edge Cases
rotate by 0 or by array length
array remains unchanged
DSA C
if (d == 0 || d == n) return; // no rotation needed
rotate empty array
no operation, safe to run
DSA C
function handles n=0 naturally by loops not running
rotate by more than array length
should reduce d modulo n before rotation (not handled in this code)
DSA C
not handled explicitly, user must provide valid d
When to Use This Pattern
When you need to shift elements in an array cyclically, use array rotation techniques to avoid repeated shifting and improve efficiency.
Common Mistakes
Mistake: Not storing the first d elements before shifting, causing data loss
Fix: Always save the first d elements in a temporary array before shifting
Mistake: Rotating by d greater than array size without modulo adjustment
Fix: Use d = d % n before rotation to handle large rotation values
Summary
Rotates an array by shifting elements left or right by a given number of positions.
Use when you want to reorder array elements cyclically without creating a new array.
The key is to save the elements that will be overwritten before shifting and then place them back at the end.