Rotate an Array Right (Set 5)

Medium

Given an array of integers nums and a whole number k, rotate the array to the right by k positions and return the result.

k may be larger than the array; rotating by nums.length is a no-op.

Rotate an Array Right (Set 5) — the data. This set runs 4 cases: nums holds arrays of 21–25 items, with values from -20 to 20; k ranges over 0–25. The hidden cases are the largest of the set.

Example 1

rotateArrayRight()
Input
nums = [9, 4, -20, 6, 19, 1, 13, 5, 2, -13, 6, -4, -18, 0, 13, -14, 5, 3, -11, 4, 10], k = 13
Output
[2, -13, 6, -4, -18, 0, 13, -14, 5, 3, -11, 4, 10, 9, 4, -20, 6, 19, 1, 13, 5]

Why

`rotateArrayRight` returns `[2, -13, 6, -4, -18, 0, 13, -14, 5, 3, -11, 4, 10, 9, 4, -20, 6, 19, 1, 13, 5]` for this input.

Example 2

rotateArrayRight()
Input
nums = [-10, 1, 5, 3, -8, 1, 20, -2, 8, 17, -3, -9, 4, 2, -5, -13, -18, 17, -20, 19, -14, -8, 2, -16, -8], k = 25
Output
[-10, 1, 5, 3, -8, 1, 20, -2, 8, 17, -3, -9, 4, 2, -5, -13, -18, 17, -20, 19, -14, -8, 2, -16, -8]

Why

`rotateArrayRight` returns `[-10, 1, 5, 3, -8, 1, 20, -2, 8, 17, -3, -9, 4, 2, -5, -13, -18, 17, -20, 19, -14, -8, 2, -16, -8]` for this input.
Constraints
0 <= nums.length <= 10^4
0 <= k <= 10^6
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

ArraysTwo Pointers

Implement rotateArrayRight in the editor and return the answer — printing it is never graded.