|
| 1 | +# Find Closest Person |
| 2 | + |
| 3 | +**Difficulty:** Easy |
| 4 | +**Category:** Math, Simulation |
| 5 | +**Leetcode Link:** [Problem Link](https://leetcode.com/problems/find-closest-person/) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 📝 Introduction |
| 10 | + |
| 11 | +We are given three integers `x`, `y`, and `z` representing positions of three people on a number line: |
| 12 | +- Person 1 is at position `x`. |
| 13 | +- Person 2 is at position `y`. |
| 14 | +- Person 3 is at position `z` (stationary). |
| 15 | + |
| 16 | +Both Person 1 and Person 2 move toward Person 3 at the same speed. |
| 17 | +We must determine which person reaches Person 3 first: |
| 18 | +- Return `1` if Person 1 arrives first. |
| 19 | +- Return `2` if Person 2 arrives first. |
| 20 | +- Return `0` if both arrive at the same time. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## 💡 Approach & Key Insights |
| 25 | + |
| 26 | +The problem reduces to comparing **absolute distances** from each person to Person 3: |
| 27 | +- `dist1 = abs(x - z)` |
| 28 | +- `dist2 = abs(y - z)` |
| 29 | + |
| 30 | +The smaller distance determines who reaches first. If both distances are equal, both reach at the same time. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## 🛠️ Breakdown of Approaches |
| 35 | + |
| 36 | +### 1️⃣ Direct Distance Comparison |
| 37 | + |
| 38 | +- **Explanation:** |
| 39 | + Compute the absolute difference in positions between each moving person and Person 3. Compare distances to decide the result. |
| 40 | +- **Time Complexity:** O(1) — Only two distance calculations and a comparison. |
| 41 | +- **Space Complexity:** O(1) — Only a few variables are used. |
| 42 | + |
| 43 | +Example: |
| 44 | +``` |
| 45 | +x = 2, y = 7, z = 4 |
| 46 | +dist1 = |2 - 4| = 2 |
| 47 | +dist2 = |7 - 4| = 3 |
| 48 | +Output: 1 (Person 1 arrives first) |
| 49 | +``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 📊 Complexity Analysis |
| 54 | + |
| 55 | +| Approach | Time Complexity | Space Complexity | |
| 56 | +| --------------- | --------------- | ---------------- | |
| 57 | +| Distance Compare| O(1) | O(1) | |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 📌 Example Walkthroughs & Dry Runs |
| 62 | + |
| 63 | +Example 1: |
| 64 | +``` |
| 65 | +Input: x = 2, y = 7, z = 4 |
| 66 | +dist1 = 2, dist2 = 3 |
| 67 | +Result: 1 |
| 68 | +``` |
| 69 | + |
| 70 | +Example 2: |
| 71 | +``` |
| 72 | +Input: x = 2, y = 5, z = 6 |
| 73 | +dist1 = 4, dist2 = 1 |
| 74 | +Result: 2 |
| 75 | +``` |
| 76 | + |
| 77 | +Example 3: |
| 78 | +``` |
| 79 | +Input: x = 1, y = 5, z = 3 |
| 80 | +dist1 = 2, dist2 = 2 |
| 81 | +Result: 0 |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## 🔗 Additional Resources |
| 87 | + |
| 88 | +- [Absolute Value in Programming](https://en.wikipedia.org/wiki/Absolute_value) |
| 89 | +- [LeetCode Simulation Problems](https://leetcode.com/tag/simulation/) |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +Author: Kailash Senthilkumar |
| 94 | +Date: 09/08/2025 |
0 commit comments