Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Rotate an array right by k

Rotate the elements of an array to the right by k steps, in-place.

Python practice8 minArrays – Fundamentals & PatternsBeginnerLast updated March 25, 2026

Problem statement

Given a list arr and a non-negative integer k, rotate arr to the right by k steps in-place and return the modified list. For example, rotating [1,2,3,4,5] by k=2 yields [4,5,1,2,3]. Your solution should handle k >= len(arr) by using k modulo len(arr).

Task

Implement an in-place right rotation of a list by k positions using O(1) extra space.

Examples

Rotate example

Input

rotate_right([1, 2, 3, 4, 5], 2)

Output

[4, 5, 1, 2, 3]

Array rotated right by 2 positions.

Input format

Two arguments: a list of integers and a non-negative integer k.

Output format

The same list rotated right by k positions and returned (list).

Constraints

- 0 <= len(arr) <= 10^5 - 0 <= k <= 10^9 - Must rotate using O(1) extra space and O(n) time

Samples

Sample 1

Input

rotate_right([1, 2, 3], 3)

Output

[1, 2, 3]

k equal to array length results in the same array.