Rotate an Array Left by One: flowchart, algorithm and program
This program reads n numbers into an array and moves every element one place to the left. The first element is saved in `first`, the others are shifted left, and the saved value is placed at the end.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read n.
- Step 3: Set i = 0.
- Step 4: If i < n, go to step 5; otherwise go to step 7.
- Step 5: Read a[i].
- Step 6: Increase i by 1. Go to step 4.
- Step 7: Set first = a[0]; i = 0.
- Step 8: If i < n - 1, go to step 9; otherwise go to step 11.
- Step 9: Set a[i] = a[i + 1].
- Step 10: Increase i by 1. Go to step 8.
- Step 11: a[n - 1] = first; i = 0.
- Step 12: If i < n, go to step 13; otherwise go to step 15.
- Step 13: Print a[i].
- Step 14: Increase i by 1. Go to step 12.
- Step 15: Stop.
Pseudocode
BEGIN
READ n
i ← 0
WHILE i < n DO
READ a[i]
i++
END WHILE
first ← a[0]; i ← 0
WHILE i < n - 1 DO
a[i] ← a[i + 1]
i++
END WHILE
a[n - 1] ← first; i ← 0
WHILE i < n DO
PRINT a[i]
i++
END WHILE
END
Program in C, Python and Java
Rotate an Array Left by One program in C
#include <stdio.h>
int main() {
int n, i, first;
int a[100];
printf("Enter n: ");
scanf("%d", &n);
printf("Enter %d numbers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
first = a[0];
for (i = 0; i < n - 1; i++) {
a[i] = a[i + 1];
}
a[n - 1] = first;
printf("Array after left rotation: ");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}Rotate an Array Left by One program in Python
n = int(input("Enter n: "))
a = list(map(int, input("Enter " + str(n) + " numbers: ").split()))
first = a[0]
for i in range(n - 1):
a[i] = a[i + 1]
a[n - 1] = first
print("Array after left rotation:", end=" ")
for i in range(n):
print(a[i], end=" ")
print()Rotate an Array Left by One program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, i, first;
System.out.print("Enter n: ");
n = sc.nextInt();
int[] a = new int[n];
System.out.print("Enter " + n + " numbers: ");
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
first = a[0];
for (i = 0; i < n - 1; i++) {
a[i] = a[i + 1];
}
a[n - 1] = first;
System.out.print("Array after left rotation: ");
for (i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
Sample input: n = 5, numbers = 1 2 3 4 5
Output: Array after left rotation: 2 3 4 5 1
How it works
- Read n and then the n numbers into the array a[0..n-1].
- Save a[0] in the variable first, because it would be overwritten by the shift and we need it later.
- Run a loop for i from 0 to n - 2 and copy a[i + 1] into a[i]. Every element moves one position to the left.
- After the loop, put first into a[n - 1], the last slot that was left free by the shift.
- Print the array from index 0 to n - 1. For 1 2 3 4 5 the result is 2 3 4 5 1.
Dry run (trace table)
| Step | Line | Box | n | i | a[0] | a[1] | a[2] | a[3] | a[4] | first | Output |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | |||||||||
| 2 | 8 | Read n | 5 | ||||||||
| 3 | 11 | i = 0 | 5 | 0 | |||||||
| 4 | 11 | i < n? | 5 | 0 | |||||||
| 5 | 12 | Read a[i] | 5 | 0 | 1 | ||||||
| 6 | 11 | i++ | 5 | 1 | 1 | ||||||
| 7 | 11 | i < n? | 5 | 1 | |||||||
| 8 | 12 | Read a[i] | 5 | 1 | 2 | ||||||
| 9 | 11 | i++ | 5 | 2 | |||||||
| 10 | 11 | i < n? | 5 | 2 | |||||||
| 11 | 12 | Read a[i] | 5 | 2 | 3 | ||||||
| 12 | 11 | i++ | 5 | 3 | |||||||
| 13 | 11 | i < n? | 5 | 3 | |||||||
| 14 | 12 | Read a[i] | 5 | 3 | 4 | ||||||
| 15 | 11 | i++ | 5 | 4 | |||||||
| 16 | 11 | i < n? | 5 | 4 | |||||||
| 17 | 12 | Read a[i] | 5 | 4 | 5 | ||||||
| 18 | 11 | i++ | 5 | 5 | |||||||
| 19 | 11 | i < n? | 5 | 5 | |||||||
| 20 | 15 | first = a[0]; i = 0 | 5 | 0 | 1 |
Time and space complexity
Time: O(n) · Space: O(1). Three separate loops each run about n times with constant work per step, and only a few extra variables are used besides the input array.
Common mistakes
- Not saving a[0] in first before shifting, so the original first value is lost and cannot be placed at the end.
- Running the shift loop up to i < n instead of i < n - 1, which reads a[n] outside the valid elements.
- Shifting in the wrong direction (from the end to the start), which overwrites values before they are copied.
Viva questions
Q1. Why do you save a[0] in the variable 'first' before the shifting loop?
The first shifting step, a[0] = a[1], overwrites a[0]. If I didn't save it, the original first element would be lost and I couldn't place it at the end.
Q2. Why does the shifting loop run only up to i < n - 1 and not i < n?
Inside the loop I read a[i + 1]. When i = n - 1, that would be a[n], which is outside the valid data, so the loop stops one step earlier.
Q3. Why must the shifting go from left to right (i = 0 upwards)?
Each element is copied from its right neighbour, so the right neighbour must still hold its original value. Going left to right guarantees that. Going right to left would overwrite values before they were copied.
Q4. Trace the program for the array 1 2 3 4 5. What is the output?
first = 1. After shifting the array is 2 3 4 5 5, then a[4] = first gives 2 3 4 5 1.
Q5. What happens if n = 1? And what happens if n = 0?
With n = 1 the shifting loop doesn't run and a[0] is set back to first, so the array is unchanged, which is correct. With n = 0, a[0] is read uninitialised and a[-1] is written, which is invalid. The program should check that n >= 1.
Q6. What if the user enters n greater than 100?
The array has size 100, so scanf would write past the end of the array. That is undefined behaviour and can crash the program. I should check n <= 100 before reading.
Q7. What is the time and space complexity of this rotation?
Time is O(n), because there is one pass to shift the elements, plus reading and printing which are also O(n). Space is O(1) extra, because I only use the single variable 'first'.
Q8. What would you change to rotate left by k positions, or to rotate right by one?
To rotate right by one, save a[n - 1], shift elements right going from the end down to index 1, and put the saved value in a[0]. To rotate left by k, I could repeat the one-step rotation k times, which is O(n*k), or use the reversal algorithm (reverse the first k, reverse the rest, reverse all), which is O(n).
Frequently asked questions
Why do we store the first element in a variable before shifting?
The first shift step does a[0] = a[1], which overwrites the original a[0]. Saving it in first lets us put it at the end after the shift.
What is the time complexity of rotating an array left by one?
It is O(n) because every element is moved once, and O(1) extra space because only one extra variable is used.
How do I rotate the array to the right by one instead?
Save the last element a[n - 1], shift elements right by looping i from n - 1 down to 1 with a[i] = a[i - 1], then put the saved value in a[0].