Skip to content

Commit c79f69f

Browse files
authored
Merge pull request #86 from SenthilkumarKailash/feature/3516-Find-Closest-Person
Solution #3516 - Kailash Senthilkumar - 09/08/2025
2 parents 0917532 + 2d1f0bc commit c79f69f

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int findClosestPerson(int x, int y, int z) {
4+
int dist1 = abs(x - z);
5+
int dist2 = abs(y - z);
6+
7+
if (dist1 < dist2) return 1;
8+
else if (dist2 < dist1) return 2;
9+
else return 0;
10+
}
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int findClosest(int x, int y, int z) {
3+
int dist1 = Math.abs(x - z);
4+
int dist2 = Math.abs(y - z);
5+
6+
if (dist1 < dist2) return 1;
7+
else if (dist2 < dist1) return 2;
8+
else return 0;
9+
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def findClosestPerson(self, x: int, y: int, z: int) -> int:
3+
dist1 = abs(x - z)
4+
dist2 = abs(y - z)
5+
6+
if dist1 < dist2:
7+
return 1
8+
elif dist2 < dist1:
9+
return 2
10+
else:
11+
return 0

0 commit comments

Comments
 (0)