Rotate an Array by One Position
Problem Statement
Given an array of integers, rotate the array by one position.
Rotation can be:
- Right rotation (last element moves to the first position), or
- Left rotation (first element moves to the last position)
Unless specified otherwise, we consider right rotation by one position.
Important Conditions
- The array must contain at least one element
- Rotation should be done in-place if possible
- The relative order of other elements must be preserved
Understanding the Problem
Example 1 (Right Rotation)
Input: [1, 2, 3, 4, 5]
Output: [5, 1, 2, 3, 4]
Explanation:
The last element 5 moves to the front, and all other elements shift one position to the right.
Example 2
Input: [10, 20, 30]
Output: [30, 10, 20]
Why This Problem Is Important
This problem helps in understanding:
- Array index manipulation
- In-place data movement
- Efficient traversal techniques
It is commonly used in:
- Coding interviews
- Data structure fundamentals
- Circular buffer and queue logic
Input and Output Format
Input
Array: [1, 2, 3, 4, 5]
Output
Rotated Array: [5, 1, 2, 3, 4]
Approach 1: Temporary Variable (Optimal)
Idea
- Store the last element
- Shift all elements one position to the right
- Place the stored element at index 0
Step-by-Step Algorithm
- Store arr[n-1] in a temporary variable
- Shift elements from index n-2 to 0 one step to the right
- Assign the temporary value to arr[0]
Pseudocode
temp = arr[n-1]
for i from n-1 down to 1:
arr[i] = arr[i-1]
arr[0] = temp
Dry Run Example
Array = [1, 2, 3, 4, 5]
temp = 5
Shift:
[1, 2, 3, 4, 4]
[1, 2, 3, 3, 4]
[1, 2, 2, 3, 4]
[1, 1, 2, 3, 4]
Place temp:
[5, 1, 2, 3, 4]
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |
Language-wise Implementation
C Implementation
#include<stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int temp = arr[n - 1];
for(int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = temp;
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
C++ Implementation
#include<iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int temp = arr[n - 1];
for(int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = temp;
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java Implementation
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int n = arr.length;
int temp = arr[n - 1];
for(int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = temp;
for(int num : arr)
System.out.print(num + " ");
}
}
Python Implementation
arr = [1, 2, 3, 4, 5]
temp = arr[-1]
for i in range(len(arr) - 1, 0, -1):
arr[i] = arr[i - 1]
arr[0] = temp
print(arr)
JavaScript Implementation
let arr = [1, 2, 3, 4, 5];
let temp = arr[arr.length - 1];
for (let i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = temp;
console.log(arr);
Common Mistakes to Avoid
- Overwriting elements without saving the last value
- Using extra arrays unnecessarily
- Confusing left rotation with right rotation
Detailed Summary
Rotating an array by one position is a fundamental array manipulation problem. By storing one element temporarily and shifting the remaining elements, we can perform the rotation efficiently in O(n) time and O(1) space. This problem builds a strong foundation for understanding more complex array rotation techniques.
Next Problem in the series
Rotate an Array by K Positions
