#754
Medium Algorithms Reach a number
Math Binary Search
44.6% acceptance
Feb 21, 2026
1936
842
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 ith 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.
Solution
Rust
Time O(n)
Space O(1)
/*
* 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 ith 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:
* 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).
* Example 2:
* 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:
* -109 <= target <= 109
* target != 0
*/
impl Solution {
pub fn reach_a_number(target: i32) -> i32 {
let target = target.unsigned_abs() as i64;
let mut sum = 0i64;
let mut n = 0i64;
loop {
n += 1;
sum += n;
if sum >= target && (sum - target) % 2 == 0 {
return n as i32;
}
}
}
}