Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Rotate Array

Rotate an array to the right by kk positions, ideally in place.

Acceptance 0%
Problem Statement

Problem

Given an integer array nums, rotate the array to the right by k steps. Each step moves the last element of the array to the front.

You should modify the array in place when possible and return the rotated array conceptually through the updated input array.

Goal

After rotation, the element originally at index i should end up at index (i + k) mod n, where n is the array length.

Input Format

  • An integer array nums
  • An integer k representing the number of right rotations

Output Format

  • The same array after performing the rotation in place

Constraints

  • 1n1051 \le n \le 10^5
  • 0k0 \le k
  • Elements may be any integers
  • Aim for O(n)O(n) time
  • Extra space should be O(1)O(1) or as small as possible
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

[5,6,7,1,2,3,4]

Explanation

Rotating right by 3 moves the last three elements to the front.

Example 2

Input

nums = [-1,-100,3,99], k = 2

Output

[3,99,-1,-100]

Explanation

After two right rotations, the last two values appear at the beginning.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.