3516. Find Closest Person LeetCode Solution
In this guide, you will get 3516. Find Closest Person LeetCode Solution with the best time and space complexity. The solution to Find Closest Person problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Closest Person solution in C++
- Find Closest Person solution in Java
- Find Closest Person solution in Python
- Additional Resources
Problem Statement of Find Closest Person
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.
Return the result accordingly.
Example 1:
Input: x = 2, y = 7, z = 4
Output: 1
Explanation:
Person 1 is at position 2 and can reach Person 3 (at position 4) in 2 steps.
Person 2 is at position 7 and can reach Person 3 in 3 steps.
Since Person 1 reaches Person 3 first, the output is 1.
Example 2:
Input: x = 2, y = 5, z = 6
Output: 2
Explanation:
Person 1 is at position 2 and can reach Person 3 (at position 6) in 4 steps.
Person 2 is at position 5 and can reach Person 3 in 1 step.
Since Person 2 reaches Person 3 first, the output is 2.
Example 3:
Input: x = 1, y = 5, z = 3
Output: 0
Explanation:
Person 1 is at position 1 and can reach Person 3 (at position 3) in 2 steps.
Person 2 is at position 5 and can reach Person 3 in 2 steps.
Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.
Constraints:
1 <= x, y, z <= 100
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
3516. Find Closest Person LeetCode Solution in C++
class Solution {
public:
int findClosest(int x, int y, int z) {
const int xz = abs(x - z);
const int yz = abs(y - z);
if (xz == yz)
return 0;
return xz < yz ? 1 : 2;
}
};
/* code provided by PROGIEZ */
3516. Find Closest Person LeetCode Solution in Java
class Solution {
public int findClosest(int x, int y, int z) {
final int xz = Math.abs(x - z);
final int yz = Math.abs(y - z);
if (xz == yz)
return 0;
return xz < yz ? 1 : 2;
}
}
// code provided by PROGIEZ
3516. Find Closest Person LeetCode Solution in Python
class Solution:
def findClosest(self, x: int, y: int, z: int) -> int:
xz = abs(x - z)
yz = abs(y - z)
if xz == yz:
return 0
return 1 if xz < yz else 2
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.