You are viewing a single comment's thread. Return to all comments →
This is pretty much more elegant than my solution of using a queue, but a question that I'm having is why use k%n? Can someone please explain this logic?
Because when the number of rotations(k) equals the size of the array(n), the array returns to it's original setup. For example, after 3 rotations, this array is back to original:
[1,2,3] 0 original
[3,1,2] 1 rotation
[2,3,1] 2 rotations
[1,2,3] 3 rotations //Back to original setup
[3,1,2] 4 rotations // same as 1 rotation
Therefore if you have 4 rotations, then 4 % 3 is 1, so you will get the same result as if you only had 1 rotation to begin with.