189. Rotate Array (Approach 1 )
Intitution
To rotate an array to the right by k
steps, we essentially want to move each element k
positions to the right, with the elements at the end wrapping around to the beginning.
A neat trick is to simulate a circular array:
If we duplicate the original array, appending it to itself, we create a structure that behaves like an infinite loop of the array.
Then, to get the rotated version, we can just extract a subarray of length
n
(original length) starting from the right offset.
This avoids doing complicated index arithmetic or multiple reversals.
Complexity
Space Complexity
Time Complexity
Code
Last updated