This problem is to rotate a given array to the right by n
steps.
For example:
Given [1, 2, 3]
and n = 1
, you should return [3, 1, 2]
Each step, the last element in the array is moved to the front of the array, and the rest are shifted right.
Another example:
Given [1, 2, 3, 4, 5]
and n = 3
, you should return [3, 4, 5, 1, 2]
- What is the time complexity of your solution? How about space?
- Can you do this in-place?
Challenge: There is an O(n) time / O(1) extra space solution.