Problem

Rotate Array by K Places

LeetCode #189Medium
Three reversals, no extra array

Rotate an array to the right by k steps, in place. k may be larger than the length of the array.

Asked atAmazonMicrosoftApple
1
2
3
4
5
6
7
[0][1][2][3][4][5][6]
Brute force · rotate by one, k times
▸
1repeat k times:
2 move the last element to the front, shifting the rest right
3return arr
state
  • k3

line 1Shift everything right by one place, 3 times over.