Rotate Array
Move the last k numbers of an array to the front, in place, with three reversals.
You are given an array of integers numskknumsk
Example 1
Output: [5, 6, 7, 1, 2, 3, 4]
Example 2
Output: [3, 99, -1, -100]
The Idea
Picture seven people standing in a line, numbered 1 to 7. The last person walks round to the front, so 7 now stands first and everyone else is one place further back. That is one step of a rotation to the right. After three such steps the line reads 5, 6, 7, 1, 2, 3, 4, the output of Example 1.
Now look at what the three steps did overall. The last three people, 5, 6 and 7, are at the front, and the first four, 1 to 4, are at the back. Inside each of these two blocks the order has not changed. Example 2 works the same way: with k = 2
So a rotation swaps the two blocks. How do we do that without a second array to copy into? It is awkward, because the blocks have different sizes: 5, 6 and 7 must go where 1, 2 and 3 are, yet 1, 2, 3, 4 is four numbers long.
Reversing helps. Reverse the whole array and it reads 7, 6, 5, 4, 3, 2, 1. The last three numbers are already at the front, only backwards, and the first four are at the back, also backwards. Look at the picture below.
The orange boxes are 5, 6 and 7, the numbers that must end up at the front. The green bracket under each row marks the part that was just reversed. In the row labelled "reverse all", reversing all seven numbers brings the orange block to the front as 7, 6, 5. In the row "reverse the first 3", it turns into 5, 6, 7. In the row "reverse the last 4", reversing those four turns 4, 3, 2, 1 into 1, 2, 3, 4, and the row is the answer.
So a rotation is three reversals: the whole array, then each of the two blocks. A reversal only swaps pairs of numbers inside the array, so it needs no second array.
Solution Approach
The simple ways to rotate are slow or need a second array. The three reversals avoid both, and leave three things to settle: what to do when k
The simple ways
What is the most direct way to rotate? Do exactly what the problem says: move the last number to the front, kk = 50,000
Another way copies the number at position i(i + k) % n%
When k is larger than the array
What if kk % nkk
Reversing one block
How do we reverse only part of the array? Put one marker, lohilohik
Where the two blocks split
After the full reversal, where does one block end and the other begin? The last kk - 1n - kkn - 1
Java Solution
The method rotate
- First, store the length of the array in
and shrinkn withk , sok %= n is less thank .n - Reverse the whole array with
.reverse(nums, 0, n - 1) - Reverse the first block, positions 0 to
, withk - 1 .reverse(nums, 0, k - 1) - Reverse the second block, positions
tok , withn - 1 . The method returns nothing, because it has changedreverse(nums, k, n - 1) itself.nums
The helper reversealohilo < hia[lo]a[hi]tmplohi
The main[1, 2, 3]kk = 7show
import java.util.Arrays;
public class RotateArray {
public static void rotate(int[] nums, int k) {
int n = nums.length;
k %= n; // rotating by n changes nothing
reverse(nums, 0, n - 1); // 1. reverse everything
reverse(nums, 0, k - 1); // 2. reverse the first k
reverse(nums, k, n - 1); // 3. reverse the rest
}
private static void reverse(int[] a, int lo, int hi) {
while (lo < hi) {
int tmp = a[lo];
a[lo++] = a[hi];
a[hi--] = tmp;
}
}
private static void show(int[] nums, int k) {
String before = Arrays.toString(nums);
rotate(nums, k);
System.out.println(before + ", k = " + k + " -> " + Arrays.toString(nums));
}
public static void main(String[] args) {
show(new int[] {1, 2, 3, 4, 5, 6, 7}, 3);
show(new int[] {-1, -100, 3, 99}, 2);
show(new int[] {1, 2, 3}, 5);
show(new int[] {1, 2, 3}, 3);
show(new int[] {1, 2, 3}, 0);
show(new int[] {42}, 7);
}
}
Running it prints:
[1, 2, 3, 4, 5, 6, 7], k = 3 -> [5, 6, 7, 1, 2, 3, 4]
[-1, -100, 3, 99], k = 2 -> [3, 99, -1, -100]
[1, 2, 3], k = 5 -> [2, 3, 1]
[1, 2, 3], k = 3 -> [1, 2, 3]
[1, 2, 3], k = 0 -> [1, 2, 3]
[42], k = 7 -> [42]
Example 1 ends as [5, 6, 7, 1, 2, 3, 4][3, 99, -1, -100][1, 2, 3]k = 5[2, 3, 1]
The next two lines leave [1, 2, 3]k = 3k %= nkk[42][42]
Leaving out k %= n[1, 2, 3]k = 5ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 3
The original implementation,
Complexity
The work is three reversals, and each reversal is a series of swaps.
- Time: O(n), for an array of n numbers, because the full reversal swaps about n / 2 pairs, and the two block reversals together swap about n / 2 more, so each number moves about twice.
- Space: O(1). Each swap uses one temporary
, and no second array is created, unlike the copy into positionint .(i + k) % n