Skip to content

Commit 7577698

Browse files
authored
Create 3516-find-closest-person.java
1 parent 8c67238 commit 7577698

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Easy/3516-find-closest-person.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* LeetCode Problem: [Custom / Example]
3+
* Title: Find Closest
4+
*
5+
* Problem Statement:
6+
* Given three integers x, y, and z, return:
7+
* - 1 if x is closer to z,
8+
* - 2 if y is closer to z,
9+
* - 0 if both are equally close.
10+
*
11+
* Example:
12+
* Input: x = 3, y = 7, z = 5
13+
* Output: 1
14+
*
15+
* Explanation: |3 - 5| = 2, |7 - 5| = 2 → equal distance → return 0
16+
*
17+
* Approach:
18+
* - Use absolute difference to measure distance from z.
19+
* - Compare xStepNeed and yStepNeed.
20+
* - Return accordingly.
21+
*
22+
* Time Complexity: O(1)
23+
* Space Complexity: O(1)
24+
*/
25+
26+
class Solution {
27+
public int findClosest(int x, int y, int z) {
28+
int xStepNeed = Math.abs(x - z);
29+
int yStepNeed = Math.abs(y - z);
30+
31+
if (xStepNeed < yStepNeed) return 1;
32+
else if (xStepNeed > yStepNeed) return 2;
33+
else return 0;
34+
}
35+
}

0 commit comments

Comments
 (0)