> ## 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.

# Move Zeroes Python Solution with Tests

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

LeetCode 283, Easy. Topics: Array, Two Pointers. [View on LeetCode](https://leetcode.com/problems/move-zeroes/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 283   # by problem number
lcpy gen -s move_zeroes   # by problem name
```

## Problem

Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.

**Note** that you must do this in-place without making a copy of the array.

### Examples

```
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Explanation: The array after moving zeroes becomes [1,3,12,0,0].
```

```
Input: nums = [0]
Output: [0]
```

### Constraints

* 1 \<= nums.length \<= 10^4
* -2^31 \<= nums\[i] \<= 2^31 - 1

**Follow up:** Could you minimize the total number of operations done?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/move_zeroes/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/move_zeroes/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Two pointers - slow tracks insertion position for next non-zero
    # Fast scans array; swap non-zero to front, preserving relative order
    # Time: O(n)
    # Space: O(1)
    def move_zeroes(self, nums: list[int]) -> None:
        slow = 0
        for fast in range(len(nums)):
            if nums[fast] != 0:
                nums[slow], nums[fast] = nums[fast], nums[slow]
                slow += 1
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

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