Skip to main content
CodeOath
← All problems

Problem

Rotate Array

Medium
  • arrays

Shift every element of nums k places to the right. An element that falls off the end comes around to the front. Return the rotated array.

Example 1
Input
nums = [10, 20, 30, 40, 50], k = 2
Output
[40, 50, 10, 20, 30]
Explanation

the last two elements, 40 and 50, wrap around to the front.

Example 2
Input
nums = [8, 9], k = 5
Output
[9, 8]
Explanation

k can be larger than the array. Moving 5 places in an array of 2 gives the same result as moving 1 place.

Constraints:

  • 1 <= nums.length <= 10^5
  • 0 <= k <= 10^9, and k may be larger than nums.length
  • -10^9 <= nums[i] <= 10^9

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.