Skip to main content
LeetCode 189, Medium. Topics: Array, Math, Two Pointers. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

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

Examples

Constraints

  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • 0 <= k <= 10^5
Follow up:
  • Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Grind, NeetCode 250, NeetCode All.
Last modified on August 25, 2026