Rotating an array is a common task in C programming that helps you understand array manipulation and how data can be rearranged efficiently. In this tutorial, we will explore multiple ways to rotate an array to the left by N
positions. You will see both simple methods for beginners and optimized approaches that use algorithms and standard library functions.
Understanding array rotation is more than just moving numbers around. It builds foundational knowledge for algorithms, memory handling, and problem-solving in C. By the end of this post, you will be able to rotate arrays confidently using different techniques, know the trade-offs between them, and choose the best method depending on your needs.
Understanding the Problem
Suppose you have an array: [10, 20, 30, 40, 50, 60, 70]
. If you rotate it left by 3 positions, the array becomes: [40, 50, 60, 70, 10, 20, 30]
. The first three elements [10, 20, 30]
are moved to the end, while the remaining elements shift left.
Program 1: Using a Temporary Array
The simplest way to rotate an array is to use a temporary array to store the first N
elements. Then, shift the remaining elements forward and copy the stored elements to the end.
#include <stdio.h>
int main() {
int n, d, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter number of positions to rotate left: ");
scanf("%d", &d);
d = d % n; // handle cases where d >= n
int temp[d];
// Copy first d elements
for(i = 0; i < d; i++) {
temp[i] = arr[i];
}
// Shift remaining elements
for(i = 0; i < n - d; i++) {
arr[i] = arr[i + d];
}
// Copy temp elements to end
for(i = 0; i < d; i++) {
arr[n - d + i] = temp[i];
}
printf("Array after rotating left by %d positions:\n", d);
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This method is easy to understand and works well for beginners. It clearly shows the logic of separating the first N
elements, shifting the remaining, and appending the saved elements.
Program 2: One-by-One Rotation
Another method is to rotate the array one element at a time. Repeat this process N
times.
#include <stdio.h>
int main() {
int n, d, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter number of positions to rotate left: ");
scanf("%d", &d);
d = d % n;
for(i = 0; i < d; i++) {
temp = arr[0];
for(j = 0; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
arr[n - 1] = temp;
}
printf("Array after rotating left by %d positions:\n", d);
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This method is simple but inefficient for large arrays or many rotations because it repeatedly moves elements one by one.
Program 3: Reversal Algorithm
The reversal algorithm rotates an array efficiently in-place with no extra space. The steps are:
- Reverse the first
N
elements. - Reverse the remaining elements.
- Reverse the entire array.
#include <stdio.h>
void reverse(int arr[], int start, int end) {
int temp;
while(start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int main() {
int n, d, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter number of positions to rotate left: ");
scanf("%d", &d);
d = d % n;
reverse(arr, 0, d - 1);
reverse(arr, d, n - 1);
reverse(arr, 0, n - 1);
printf("Array after rotating left by %d positions:\n", d);
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This method is fast and uses no extra space. It is one of the most efficient techniques for array rotation.
Program 4: Using memcpy()
for Efficient Rotation
The C standard library’s memcpy()
can copy blocks of memory quickly. We can use it to rotate an array efficiently:
#include <stdio.h>
#include <string.h>
int main() {
int n, d, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter number of positions to rotate left: ");
scanf("%d", &d);
d = d % n;
int temp[d];
memcpy(temp, arr, d * sizeof(int));
memcpy(arr, arr + d, (n - d) * sizeof(int));
memcpy(arr + (n - d), temp, d * sizeof(int));
printf("Array after rotating left by %d positions:\n", d);
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This method is concise, fast, and uses optimized library functions while maintaining clarity.
Performance Comparison
Method | Time Complexity | Space Complexity | Notes |
---|---|---|---|
Temporary Array | O(n) | O(d) | Simple and easy to understand, uses extra space for d elements. |
One-by-One Shift | O(n × d) | O(1) | Inefficient for large arrays, simple to follow. |
Reversal Algorithm | O(n) | O(1) | Very efficient, no extra space required. |
memcpy() with Temp Array | O(n) | O(d) | Fast in practice, concise code using standard library functions. |
Which Method Should You Use?
If you are just starting out, the temporary array method is the easiest to understand. It clearly shows the logic of separating the first N
elements, shifting the rest, and appending the saved elements.
The one-by-one shift method is simple but inefficient for larger arrays or many rotations. It is good for small practice problems but not recommended in real applications.
The reversal algorithm is the best choice for efficiency. It is fast, uses no extra space, and is often preferred in technical interviews or performance-critical code.
The memcpy()
method is ideal if you are comfortable with standard library functions and want optimized code. It combines clarity with speed but still requires temporary storage for d
elements.
In practice, the reversal algorithm and memcpy()
approach are the most commonly used for real-world applications.
FAQs
Q1: What is the time complexity of each method?
The time complexity depends on how the elements are moved:
- Temporary array method: O(n) — Each element is accessed once, and the first
d
elements are stored temporarily. - One-by-one rotation: O(n × d) — Each of the
d
rotations moves almost all elements, making it slower for larger arrays or rotations. - Reversal algorithm: O(n) — Each element is swapped a fixed number of times, giving efficient performance even for large arrays.
memcpy()
method: O(n) — The library function efficiently copies blocks of memory, similar to the temporary array approach but optimized in practice.
Q2: Which method is best?
For small arrays or learning purposes, all methods work fine. For large arrays or frequent rotations, the reversal algorithm is the most efficient as it uses minimal extra space and completes in linear time. The memcpy()
approach is also fast if you prefer using standard library functions.
Q3: Can we rotate an array to the right instead of left?
Yes, right rotation is possible. You can reverse the logic of left rotation: instead of shifting elements left, shift them right. The reversal algorithm can also be adapted by reversing the last d
elements first, then the first n - d
elements, and finally the entire array.
Conclusion
Rotating an array to the left is a fundamental operation that improves your understanding of arrays and efficiency trade-offs. The temporary array method is simple and easy to grasp, while the one-by-one method is intuitive but slow for large data. The reversal algorithm provides an elegant and efficient approach with minimal space, and the memcpy()
method demonstrates how standard library functions can make your code concise and fast.
By practicing these techniques, you strengthen your knowledge of arrays in C and prepare yourself for more advanced programming challenges.
References & Additional Resources
A curated collection of textbooks, tutorials, and documentation for understanding array operations, rotation algorithms, and related C functions.
- Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language, 2nd Edition, Prentice Hall, 1988 – Foundational text covering arrays, pointers, functions, and core C programming concepts.
- GeeksforGeeks – Array Rotation – Explains different methods to rotate arrays, including the juggling and reversal algorithms, with examples in C.
- C Programming – memcpy() Function – Guide to using
memcpy()
for copying blocks of memory, useful in array manipulations. - cplusplus.com: memcpy() – Official documentation for the
memcpy()
function used in copying memory blocks, useful for array operations. - Programiz: C Examples – Practical examples of array operations and common C programming use cases.
- TutorialsPoint: Arrays in C – Introductory guide covering array declaration, initialization, traversal, and manipulation.