Practice/Microsoft/Leetcode 189. Rotate Array
CodingOptional
Given an integer array and a non-negative integer k, shift all elements in the array to the right by k positions. Elements that move beyond the end of the array should wrap around to the beginning.
For example, if you have [1, 2, 3, 4, 5] and shift right by 2, element at index 3 (value 4) moves to index 5, which wraps to index 0, giving [4, 5, 1, 2, 3].
Important: Modify the array in-place and return nothing. The function should directly alter the input array.
None (Python) or void (JavaScript/TypeScript)1 <= nums.length <= 10^5-2^31 <= nums[i] <= 2^31 - 10 <= k <= 10^5Example 1:
` Input: nums = [1, 2, 3, 4, 5], k = 2 Output: [4, 5, 1, 2, 3] Explanation:
Example 2:
` Input: nums = [-1, -100, 3, 99], k = 2 Output: [3, 99, -1, -100] Explanation:
Example 3:
Input: nums = [1, 2, 3], k = 4 Output: [3, 1, 2] Explanation: Since k=4 and length=3, effective rotation is k % 3 = 1