Find Closest Person - Problem

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 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
Find Closest Person Problem OverviewNumber Line1Person 1x = 12Person 2y = 43Person 3z = 3Distance = 2Distance = 1ComparisonDistance 1: |1-3| = 2Distance 2: |4-3| = 11 < 2, so Person 2 winsOutput: 2
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
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen