Cyclically Shift an Array by K Steps
Implement rotateArray
Given an array
nums and an integer k, return the array after rotating it to the right by k positions — each element moves k spots to the right, wrapping around to the front when it runs past the end.
k may be larger than the array's length, so start by reducing it with k % nums.length before rotating.
Example 1:
Input: nums = [11,22,33,44,55], k = 2
Output: [44,55,11,22,33]
Example 2:
Input: nums = [5,6,7], k = 0
Output: [5,6,7]
Example 3:
Input: nums = [1,2], k = 3
Output: [2,1]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 10⁵ - ●
-10⁹ ≤ nums[i] ≤ 10⁹ - ●
0 ≤ k ≤ 10⁵
nums =
[11, 22, 33, 44, 55]
k =
2