Reach a Number

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.

Example 1
Inputtarget = 2
Output3
On the first move you go from 0 to 1, on the second from 1 to -1, and on the third from -1 to 2.
Example 2
Inputtarget = 3
Output2
On the first move you go from 0 to 1, and on the second from 1 to 3.

Constraints

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

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate