C Program to Rotate Array Elements
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
How to Think About It
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
Code
#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; }
Dry Run
Let's trace rotating the array [1, 2, 3, 4, 5] by 2 positions through the code.
Store first d elements
temp = [1, 2]
Shift remaining elements left
arr after shift = [3, 4, 5, 4, 5]
Copy temp elements to end
arr final = [3, 4, 5, 1, 2]
| Index | Value after rotation step |
|---|---|
| 0 | 3 |
| 1 | 4 |
| 2 | 5 |
| 3 | 1 |
| 4 | 2 |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Temporary array | O(n) | O(d) | Simple and easy to understand |
| Rotate one by one | O(n*d) | O(1) | Small arrays or small rotations |
| Reversal algorithm | O(n) | O(1) | Efficient in-place rotation |