# I dont understand the test case in rotate array

• when the input is [1,2], 1, the expected result is [2,1].
When the input is[1,2,3], 1, the expected result should be [2, 3, 1], however it is [3,1,2]. Now I am confused about what rotation means? Doesnt it mean shift the first element to the last slot, and move the second to be the first?

• When the input is [1 2 3 4 5 6 7], k = 3, we do the shift as follows:

[1 2 3 4] [5 6 7] (the second subarray has the last k elements)

swap the two subarrays to get the resulted array
=> [5 6 7] [1 2 3 4] => [5 6 7 1 2 3 4]

Thus, in your example, the input is [1 2 3], k = 1, we do the shift as follows:

[1 2] [3] (the second subarray has the last k elements)

swap the two subarrays to get the resulted array
=> [3] [1 2] => [3 1 2]

• Can you explain why [1, 2], 3 returns [2, 1] please?

• Rotate an array of n elements to the right by n, 2n, 3n, ... steps will result in an array same as before. So, rotating the array [1, 2] to the right by 3 steps is equivalent to rotating it to the right by 3 % 2 = 1 step. Then

[1] [2] (the second subarray has the last 1 element)

swap the two subarrays to get the resulted array => [2] [1] => [2 1]

• Thanks, got it!

• Thanks a lot!

• @youjia2 , for your question "why [1, 2], 3 returns [2, 1]?". Try rotating 3 times. First rotation 2,1 then second rotation 1,2 and then third rotation again 2,1 hope it helps.

• @yswu1234 thank you a lot! Finally understood

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.