Find Closest Person - Problem
You are given three integers x, y, and z, representing the positions of three people on a number line:
xis the position of Person 1yis the position of Person 2zis 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
Input & Output
Example 1 — Person 2 Closer
$
Input:
x = 1, y = 4, z = 3
›
Output:
2
💡 Note:
Distance from Person 1 to Person 3: |1-3| = 2. Distance from Person 2 to Person 3: |4-3| = 1. Person 2 is closer, so return 2.
Example 2 — Equal Distance
$
Input:
x = 2, y = 6, z = 4
›
Output:
0
💡 Note:
Distance from Person 1 to Person 3: |2-4| = 2. Distance from Person 2 to Person 3: |6-4| = 2. Both distances are equal, so return 0.
Example 3 — Person 1 Closer
$
Input:
x = 10, y = 2, z = 8
›
Output:
1
💡 Note:
Distance from Person 1 to Person 3: |10-8| = 2. Distance from Person 2 to Person 3: |2-8| = 6. Person 1 is closer, so return 1.
Constraints
- -109 ≤ x, y, z ≤ 109
- All positions can be anywhere on the number line
Visualization
Tap to expand
Understanding the Visualization
1
Input
Three people at positions x, y, and z on number line
2
Process
Calculate distances and find who is closest to Person 3
3
Output
Return 1, 2, or 0 based on who arrives first
Key Takeaway
🎯 Key Insight: Distance determines winner when speed is equal - compare absolute differences
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code