Reach a Number
Real-world version: Landing Exactly on the MarkerThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def reach_number(marker: int) -> int:public int reachNumber(int marker)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's