Skip to main content
LeetCode 754, Medium. Topics: Math, Binary Search. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 46 parametrized pytest cases, and a playground notebook:

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

Constraints

  • -10^9 <= target <= 10^9
  • target != 0

Solution

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

Complexity

Tags

Last modified on September 7, 2026