📘
Rotate Array
EasyArrays & Sorting
Given an integer array nums, rotate the array to the right by k steps.
Output the rotated array as space-separated integers.
Example:
Input:
1,2,3,4,5,6,7 3
Output:
5 6 7 1 2 3 4
Reasoning:
- The input array is 1,2,3,4,5,6,7 and we need to rotate it to the right by k=3 steps.
- To rotate the array, we split it into two parts: the last k elements (5,6,7) and the rest of the array (1,2,3,4).
- We then concatenate these two parts in reverse order, resulting in the rotated array: 5,6,7,1,2,3,4.
- The final output is the rotated array as space-separated integers: 5671234.
Constraints:
- 1 <= len(nums) <= 10^5
- -2^31 <= nums[i] <= 2^31 - 1
- 0 <= k <= 10^5
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.