8. Rotate Array by K Steps

EasyArrayArray

Given an integer array nums, rotate the array to the right by k steps.

The rotation must be done in-place.

Input: An integer array nums of length n, and a non-negative integer k.

Output: The array nums after rotating right by k steps.

Examples

Example 1
Input: [1,2,3,4,5,6,7],3
Output: [5,6,7,1,2,3,4]
Explanation: Step 1: [7,1,2,3,4,5,6]. Step 2: [6,7,1,2,3,4,5]. Step 3: [5,6,7,1,2,3,4].
Example 2
Input: [-1,-100,3,99],2
Output: [3,99,-1,-100]
Explanation: After 1 step: [99,-1,-100,3]. After 2 steps: [3,99,-1,-100].
Example 3
Input: [1,2,3,4,5],0
Output: [1,2,3,4,5]
Explanation: k=0 — no rotation. Array is unchanged.

Constraints

Asked by

AppleAccentureCapgeminiBloombergMicrosoftAmazon
Solve this problem in the editor →