Skip to main content

Rotate Array

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

Problems8 / 152
Sign in

Rotate Array

MEDIUMArrayMath

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

Example 1

Input:  nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]

Example 2

Input:  nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Show hint

Reverse the entire array, then reverse the first k elements, then reverse the remaining elements. Or use extra space to store rotated elements.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
nums =
[1,2,3,4,5,6,7]
k =
3
Expected
[5,6,7,1,2,3,4]
Run with your own input (JSON)