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.
Determine which person reaches Person 3 first:
Return the result accordingly.
Example 1:
Input: x = 2, y = 7, z = 4
Output: 1
Explanation:
Since Person 1 reaches Person 3 first, the output is 1.
Example 2:
Input: x = 2, y = 5, z = 6
Output: 2
Explanation:
Since Person 2 reaches Person 3 first, the output is 2.
Example 3:
Input: x = 1, y = 5, z = 3
Output: 0
Explanation:
Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.
Constraints:
1 <= x, y, z <= 100Problem Overview: Two people stand at positions x and y on a number line. A third position z is the target. The task is to determine which person is closer to z. If both are equally close, return 0; otherwise return the index of the closer person.
Approach 1: Step-by-Step Simulation (O(d) time, O(1) space)
A straightforward way is to simulate both people moving one step at a time toward the target position z. Each step reduces the distance between the person and the target until one reaches it first. If both reach the same step count, the result is a tie. This approach models the movement literally but performs unnecessary iterations proportional to the distance d from each person to z. Since the number line distance could be large, repeatedly decrementing or incrementing values is inefficient for a problem that only requires comparing distances.
Approach 2: Absolute Distance Comparison (O(1) time, O(1) space)
The optimal solution comes directly from math. The distance between two points on a number line is the absolute difference: |a - b|. Compute d1 = |x - z| and d2 = |y - z|. If d1 < d2, person 1 is closer. If d2 < d1, person 2 is closer. If the distances are equal, both reach the target at the same time and the result is 0.
This works because movement on a number line is symmetric. Whether someone approaches from the left or right, the minimum number of steps required to reach z equals the absolute difference. The comparison therefore reduces the entire problem to two constant-time arithmetic operations and a conditional check.
The algorithm performs a couple of integer subtractions and calls to abs(), so the runtime stays constant regardless of input values. Memory usage is also constant because only a few variables are stored.
Problems like this appear frequently in beginner algorithm rounds because they test your understanding of mathematical reasoning and simple implementation. Recognizing that the problem reduces to comparing absolute distances avoids unnecessary simulation or loops.
Recommended for interviews: Use the absolute distance comparison. Interviewers expect you to recognize that distance on a number line is |a - b|. Mentioning the simulation idea briefly shows basic reasoning, but the constant-time mathematical comparison demonstrates clean problem reduction and strong fundamentals.
We calculate the distance a between the 1st person and the 3rd person, and the distance b between the 2nd person and the 3rd person.
a = b, it means both people arrive at the same time, return 0;a \lt b, it means the 1st person will arrive first, return 1;2.The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Step-by-Step Simulation | O(d) | O(1) | Conceptual understanding of movement toward a target on a number line |
| Absolute Distance Comparison (Mathematics) | O(1) | O(1) | Best approach for interviews and production code; directly compares |x - z| and |y - z| |
LeetCode#3516 Find Closest Person - Python • CodeJulian • 1,501 views views
Watch 9 more video solutions →Practice Find Closest Person with our built-in code editor and test cases.
Practice on FleetCode