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

# Reach a Number Python Solution with Tests

> Tested Python solution for LeetCode 754 with 46 pytest cases. Generate a practice environment with lcpy.

LeetCode 754, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/reach-a-number/description/).

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

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

## Problem

You are standing at position `0` on an infinite number line. There is a destination at position `target`.

You can make some number of moves `numMoves` so that:

* On each move, you can either go left or right.
* During the `i^th` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen direction.

Given the integer `target`, return *the **minimum** number of moves required (i.e., the minimum `numMoves`)* to reach the destination.

### Examples

```
Input: target = 2
Output: 3
Explanation:
On the 1st move, we step from 0 to 1 (1 step).
On the 2nd move, we step from 1 to -1 (2 steps).
On the 3rd move, we step from -1 to 2 (3 steps).
```

```
Input: target = 3
Output: 2
Explanation:
On the 1st move, we step from 0 to 1 (1 step).
On the 2nd move, we step from 1 to 3 (2 steps).
```

### Constraints

* `-10^9 <= target <= 10^9`
* `target != 0`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sqrt(target)) - the loop runs about sqrt(2 * |target|) times
    # Space: O(1)
    def reach_number(self, target: int) -> int:
        target = abs(target)
        moves = 0
        total = 0
        while total < target or (total - target) % 2:
            moves += 1
            total += moves
        return moves
```

## Complexity

| Time | Space | | |
| - | - | - | - |
| O(sqrt(target)) - the loop runs about sqrt(2 \* | target | ) times | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.