Watch 10 video solutions for Find Closest Person, a easy level problem involving Math. This walkthrough by CodeJulian has 1,501 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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| |