All problems
0992MediumMathBinary Search

Landing Exactly on the Marker

Tracked in this browser only
Write code

Trains the technique from

LeetCode 754Reach a Number

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A pointer sits at position 0 on a line of whole numbers. On move number k, counting the first move as one, the pointer travels exactly k places, and each move may be taken to the left or to the right.

Return the fewest moves that leave the pointer sitting exactly on marker.

Examples

Example 1

Input
marker = 4
Output
3

One move right, then one left, then one right lands on the marker after three moves. Two moves cannot manage it, because they land on one place away from the start or three away, never four.

Example 2

Input
marker = -1
Output
1

A single move to the left arrives straight away.

Example 3

Input
marker = 11
Output
5

Four moves all rightward reach ten, one short, and no turning of them can shave a single place off, since turning a move around always shifts the finish by an even amount. Five moves rightward overshoot by four, and turning the second move around takes exactly four back.

Constraints

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

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def reach_number(marker: int) -> int:
Java
public int reachNumber(int marker)
September 7
Apply