3516. Find Closest Person
Problem Description
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.
Intuition
To determine which person reaches the stationary Person 3 first, we need to consider the distance each moving person starts from. The distance is calculated as the absolute difference between their starting position and Person 3's position (z
).
- For Person 1, the distance to travel is
|x - z|
. - For Person 2, the distance to travel is
|y - z|
.
The person with the smaller distance will reach Person 3 first. Therefore, we compare these distances:
- If
|x - z|
equals|y - z|
, it means both persons arrive simultaneously, so we return0
. - If
|x - z|
is less than|y - z|
, Person 1 reaches first, so return1
. - If
|y - z|
is less than|x - z|
, Person 2 reaches first, so return2
.
This approach involves simple arithmetic calculations ensuring efficient comparison of distances.
Learn more about Math patterns.
Solution Approach
The solution leverages basic mathematical computation to determine who reaches Person 3 first. The steps involved are as follows:
-
Calculate Distances:
- Let
a
be the distance from Person 1 to Person 3, computed asa = abs(x - z)
. - Let
b
be the distance from Person 2 to Person 3, computed asb = abs(y - z)
.
- Let
-
Comparison of Distances:
- If
a == b
, it indicates that both Person 1 and Person 2 will reach Person 3 simultaneously. Thus, return0
. - If
a < b
, Person 1 is closer to Person 3 than Person 2 is, so return1
. - Otherwise, if
b < a
, Person 2 reaches Person 3 first, so return2
.
- If
This approach efficiently resolves the problem with constant time complexity, as it only involves basic arithmetic operations and comparisons. The solution is implemented in the findClosest
method in the reference code:
class Solution:
def findClosest(self, x: int, y: int, z: int) -> int:
a = abs(x - z)
b = abs(y - z)
return 0 if a == b else (1 if a < b else 2)
This code effectively computes the required distances and returns the desired output based on their comparison.
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's walk through an example to understand how the solution approach is applied. Suppose we have three integers representing positions on a number line:
x = 5
(Position of Person 1)y = 2
(Position of Person 2)z = 4
(Position of Person 3, stationary)
Using the solution approach, let's determine which person reaches Person 3 first:
-
Calculate Distances:
- Distance
a
from Person 1 to Person 3:
[ a = \lvert x - z \rvert = \lvert 5 - 4 \rvert = 1 ] - Distance
b
from Person 2 to Person 3:
[ b = \lvert y - z \rvert = \lvert 2 - 4 \rvert = 2 ]
- Distance
-
Comparison of Distances:
- Compare
a
andb
:- Since
a < b
(1 < 2), Person 1 is closer to Person 3.
- Since
- Thus, Person 1 reaches Person 3 first, and we return
1
.
- Compare
This process shows that Person 1 will arrive at Person 3's position before Person 2, given the calculated distances.
Solution Implementation
1class Solution:
2 def findClosest(self, x: int, y: int, z: int) -> int:
3 # Calculate the absolute difference between x and z
4 difference_x = abs(x - z)
5
6 # Calculate the absolute difference between y and z
7 difference_y = abs(y - z)
8
9 # If both differences are equal, return 0
10 if difference_x == difference_y:
11 return 0
12
13 # If x is closer to z than y, return 1
14 elif difference_x < difference_y:
15 return 1
16
17 # If y is closer to z than x, return 2
18 else:
19 return 2
20
1class Solution {
2 // This method finds which one of x or y is closer to z.
3 public int findClosest(int x, int y, int z) {
4 // Calculate the absolute difference between x and z.
5 int distanceX = Math.abs(x - z);
6 // Calculate the absolute difference between y and z.
7 int distanceY = Math.abs(y - z);
8 // If the distances are equal, return 0.
9 // Otherwise, return 1 if x is closer to z, or 2 if y is closer to z.
10 return distanceX == distanceY ? 0 : (distanceX < distanceY ? 1 : 2);
11 }
12}
13
1#include <cmath> // Include cmath for the abs() function
2
3class Solution {
4public:
5 // This function determines whether 'x' or 'y' is closer to 'z'.
6 int findClosest(int x, int y, int z) {
7 // Calculate the absolute difference between x and z
8 int diffX = std::abs(x - z);
9 // Calculate the absolute difference between y and z
10 int diffY = std::abs(y - z);
11
12 // If the differences are the same, return 0 indicating equidistance
13 if (diffX == diffY) {
14 return 0;
15 }
16
17 // If x is closer to z than y is, return 1
18 if (diffX < diffY) {
19 return 1;
20 }
21
22 // Otherwise, y is closer to z, return 2
23 return 2;
24 }
25};
26
1function findClosest(x: number, y: number, z: number): number {
2 // Calculate the absolute difference between x and z
3 const distanceX = Math.abs(x - z);
4
5 // Calculate the absolute difference between y and z
6 const distanceY = Math.abs(y - z);
7
8 // Compare distances: Return 0 if equal, 1 if x is closer, 2 if y is closer
9 return distanceX === distanceY ? 0 : distanceX < distanceY ? 1 : 2;
10}
11
Time and Space Complexity
The time complexity of the provided code is O(1)
because the computation involves a constant number of arithmetic operations (calculating absolute differences and comparing them), which does not depend on the size of the input.
The space complexity is also O(1)
as the code uses a fixed amount of extra space for variables a
and b
regardless of the input size.
Learn more about how to find time and space complexity quickly.
Which of the following array represent a max heap?
Recommended Readings
Math for Technical Interviews How much math do I need to know for technical interviews The short answer is about high school level math Computer science is often associated with math and some universities even place their computer science department under the math faculty However the reality is that you
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https assets algo monster recursion jpg You first call Ben and ask
Want a Structured Path to Master System Design Too? Don’t Miss This!