Rotate an Array In Place
Problem statement
Rotate nums to the right by k positions. Modify the given array and return it. A right rotation moves the last element to the front.
Use O(n) time and O(1) extra space, where n is the array length.
Function
rotateArray(nums: int[], k: int) → int[]Examples
Example 1
nums = [1,2,3,4,5,6,7]k = 3return = [5,6,7,1,2,3,4]The last three values, [5,6,7], move before [1,2,3,4].
Example 2
nums = [-1,-100,3,99]k = 2return = [3,99,-1,-100]The last two values move to the front, preserving the order inside each part.
Constraints
- For this exercise, assume 1 <= nums.length <= 100000.
- For this exercise, assume -10^9 <= nums[i] <= 10^9.
- For this exercise, assume 0 <= k <= 10^9.
Source note: Original OA task images and any included source example. The practice contract follows the rules stated in the problem and source note.