#3516
Easy Algorithms Find closest person
Math
88.9% acceptance
Feb 25, 2026
426
47
You are given three integers x, y, and z, representing the positions of three people on a number line:
x is the position of Person 1.
y is the position of Person 2.
z is the position of Person 3, who does not move.
Both Person 1 and Person 2 move toward Person 3 at the same speed.
Return 1 if Person 1 arrives first, 2 if Person 2 arrives first, 0 if both arrive at the same time.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn find_closest(x: i32, y: i32, z: i32) -> i32 {
let d1 = (x - z).abs();
let d2 = (y - z).abs();
match d1.cmp(&d2) {
std::cmp::Ordering::Less => 1,
std::cmp::Ordering::Greater => 2,
std::cmp::Ordering::Equal => 0,
}
}
}