Find Closest Person - Solution & Explanation
Problem Statement
You are given three integers x, y, and z, representing the positions of three people on a number line:
xis the position of Person 1.yis the position of Person 2.zis 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 1 if Person 1 arrives first.
- Return 2 if Person 2 arrives first.
- Return 0 if both arrive at the same time.
Return the result accordingly.
Example 1:
Input: x = 2, y = 7, z = 4
Output: 1
Explanation:
- Person 1 is at position 2 and can reach Person 3 (at position 4) in 2 steps.
- Person 2 is at position 7 and can reach Person 3 in 3 steps.
Since Person 1 reaches Person 3 first, the output is 1.
Example 2:
Input: x = 2, y = 5, z = 6
Output: 2
Explanation:
- Person 1 is at position 2 and can reach Person 3 (at position 6) in 4 steps.
- Person 2 is at position 5 and can reach Person 3 in 1 step.
Since Person 2 reaches Person 3 first, the output is 2.
Example 3:
Input: x = 1, y = 5, z = 3
Output: 0
Explanation:
- Person 1 is at position 1 and can reach Person 3 (at position 3) in 2 steps.
- Person 2 is at position 5 and can reach Person 3 in 2 steps.
Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.
Constraints:
1 <= x, y, z <= 100
Approach Overview
Problem 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.
Solution
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.
- If
a = b, it means both people arrive at the same time, return0; - If
a \lt b, it means the 1st person will arrive first, return1; - Otherwise, it means the 2nd person will arrive first, return
2.
The time complexity is O(1), and the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
Detailed Complexity Analysis
| 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| |
Video Solution
3516. Find Closest Person (Leetcode Easy) • Programming Live with Larry • 998 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Find Closest Person easy or hard?
Find Closest Person Python/Java solution
How to solve Find Closest Person in O(1)?
What is the best approach for Find Closest Person?
Is Find Closest Person asked at Google/Amazon/Meta?
What data structure is used in Find Closest Person?
What is the time complexity of Find Closest Person?
Ready to solve this problem?
Practice Find Closest Person with our built-in code editor and test cases.
Practice on FleetCode