> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Rotate Array Python Solution with Tests

> Tested Python solution for LeetCode 189 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 189, Medium. Topics: Array, Math, Two Pointers. [View on LeetCode](https://leetcode.com/problems/rotate-array/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 189   # by problem number
lcpy gen -s rotate_array   # by problem name
```

## Problem

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

### Examples

```
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
```

```
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/rotate_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/rotate_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) - three passes through array
    # Space: O(1) - in-place rotation using reversal
    def rotate(self, nums: list[int], k: int) -> None:
        """
        Rotate array right by k steps using triple reversal.

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

        Step 1: Reverse entire array
        [1,2,3,4,5,6,7] → [7,6,5,4,3,2,1]

        Step 2: Reverse first k elements
        [7,6,5,4,3,2,1] → [5,6,7,4,3,2,1]
                ↑k=3↑

        Step 3: Reverse remaining elements
        [5,6,7,4,3,2,1] → [5,6,7,1,2,3,4] ✓
              ↑remaining↑
        """
        n = len(nums)
        k = k % n  # Handle k > n

        def reverse(start: int, end: int) -> None:
            while start < end:
                nums[start], nums[end] = nums[end], nums[start]
                start += 1
                end -= 1

        # Step 1: Reverse entire array
        reverse(0, n - 1)
        # Step 2: Reverse first k elements
        reverse(0, k - 1)
        # Step 3: Reverse remaining elements
        reverse(k, n - 1)
```

## Complexity

| Time                              | Space                                   |
| --------------------------------- | --------------------------------------- |
| O(n) - three passes through array | O(1) - in-place rotation using reversal |

## Tags

[Grind](/catalog/grind), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
