Java Program to Rotate Array with Output and Explanation
k positions using a temporary array or reverse parts of the array; for example, rotate(int[] arr, int k) shifts elements right by k places.Examples
How to Think About It
Algorithm
Code
public class RotateArray { public static void rotate(int[] arr, int k) { int n = arr.length; k = k % n; int[] temp = new int[k]; for (int i = 0; i < k; i++) { temp[i] = arr[n - k + i]; } for (int i = n - 1; i >= k; i--) { arr[i] = arr[i - k]; } for (int i = 0; i < k; i++) { arr[i] = temp[i]; } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int k = 2; rotate(arr, k); for (int num : arr) { System.out.print(num + " "); } } }
Dry Run
Let's trace rotating the array [1, 2, 3, 4, 5] by 2 positions.
Calculate effective rotation
k = 2 % 5 = 2
Save last k elements
temp = [4, 5]
Shift elements right by k
arr becomes [1, 2, 3, 3, 4]
Copy saved elements to front
arr becomes [4, 5, 1, 2, 3]
| Iteration | Array State |
|---|---|
| Initial | [1, 2, 3, 4, 5] |
| Save last 2 | temp = [4, 5] |
| Shift right | [1, 2, 3, 3, 4] |
| Copy temp front | [4, 5, 1, 2, 3] |
Why This Works
Step 1: Calculate k modulo array length
Using k % n ensures rotation does not exceed array size and handles cases where k is larger than the array.
Step 2: Save last k elements
We temporarily store the last k elements to avoid overwriting them during shifting.
Step 3: Shift elements right
Elements before the last k are moved right by k positions to make space at the front.
Step 4: Place saved elements at front
Finally, the saved elements are copied to the start, completing the rotation.
Alternative Approaches
public class RotateArray { public static void reverse(int[] arr, int start, int end) { while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } public static void rotate(int[] arr, int k) { int n = arr.length; k = k % n; reverse(arr, 0, n - 1); reverse(arr, 0, k - 1); reverse(arr, k, n - 1); } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int k = 2; rotate(arr, k); for (int num : arr) { System.out.print(num + " "); } } }
public class RotateArray { public static int[] rotate(int[] arr, int k) { int n = arr.length; k = k % n; int[] rotated = new int[n]; for (int i = 0; i < n; i++) { rotated[(i + k) % n] = arr[i]; } return rotated; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int k = 2; int[] result = rotate(arr, k); for (int num : result) { System.out.print(num + " "); } } }
Complexity: O(n) time, O(k) or O(1) depending on method space
Time Complexity
The rotation requires moving each element once, so it takes O(n) time where n is the array length.
Space Complexity
Using a temporary array for k elements uses O(k) space; the reversal method uses O(1) extra space.
Which Approach is Fastest?
All methods run in O(n) time, but the reversal method is fastest in space as it rotates in-place without extra arrays.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Temporary array | O(n) | O(k) | Simple and clear for small k |
| Array reversal | O(n) | O(1) | In-place rotation with minimal space |
| New array | O(n) | O(n) | Easy to understand but uses extra memory |