Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Rotate Array

XPChallenge Points: 20
levelLevel: Medium

You are given an integer array nums and an integer k.

Your task is to rotate the array to the right by k steps, where k is non-negative.

A right rotation means that the last k elements of the array move to the front,
and all other elements shift to the right.
You must modify the array in-place, without using extra space for another array.

Example 1:

Input: nums = [10, 20, 30, 40, 50, 60], k = 2

Output: [50, 60, 10, 20, 30, 40]

Explanation:

After 1 rotation → [60, 10, 20, 30, 40, 50] After 2 rotations → [50, 60, 10, 20, 30, 40]

Example 2:

Input: nums = [5, 8, 9, 12, 15], k = 3

Output: [9, 12, 15, 5, 8]

Explanation:

After 1 rotation → [15, 5, 8, 9, 12] After 2 rotations → [12, 15, 5, 8, 9] After 3 rotations → [9, 12, 15, 5, 8]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result