LeetCode Problem Workspace

Find Closest Person

Determine which of two people reaches a third person first using a simple math-driven distance comparison strategy.

category

1

Topics

code_blocks

8

Code langs

hub

3

Related

Practice Focus

Easy · Math-driven solution strategy

bolt

Answer-first summary

Determine which of two people reaches a third person first using a simple math-driven distance comparison strategy.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math-driven solution strategy

Try AiBox Copilotarrow_forward

This problem requires comparing the distances of two people to a third person on a number line. By calculating the absolute differences and evaluating which distance is smaller, you can determine who reaches the target first. If both distances are equal, it indicates a tie, returning zero.

Problem Statement

You are given three integers x, y, and z representing positions of Person 1, Person 2, and Person 3 on a straight line. Both Person 1 and Person 2 move directly toward Person 3 at the same speed.

Your task is to determine which person reaches Person 3 first. Return 1 if Person 1 arrives first, 2 if Person 2 arrives first, or 0 if both arrive at the same time.

Examples

Example 1

Input: x = 2, y = 7, z = 4

Output: 1

Since Person 1 reaches Person 3 first, the output is 1.

Example 2

Input: x = 2, y = 5, z = 6

Output: 2

Since Person 2 reaches Person 3 first, the output is 2.

Example 3

Input: x = 1, y = 5, z = 3

Output: 0

Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.

Constraints

  • 1 <= x, y, z <= 100

Solution Approach

Compute Distances

Calculate the absolute difference between x and z, and y and z. This gives the distance each person must travel to reach Person 3.

Compare Distances

Compare the computed distances. If Person 1's distance is smaller, return 1; if Person 2's distance is smaller, return 2; if distances are equal, return 0.

Return Result

Output the result immediately after comparison, ensuring the solution is efficient and avoids unnecessary loops or condition checks.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(1) because distance calculation and comparison involve only basic arithmetic. Space complexity is O(1) since no extra storage beyond a few variables is needed.

What Interviewers Usually Probe

  • The problem expects a direct math-driven approach rather than iterative simulation.
  • Watch for off-by-one errors when calculating distances along the number line.
  • Clarify handling of ties to avoid ambiguous return values.

Common Pitfalls or Variants

Common pitfalls

  • Ignoring absolute value when calculating distances can lead to incorrect results.
  • Assuming Person 1 is always closer without proper comparison.
  • Returning wrong values in tie scenarios instead of zero.

Follow-up variants

  • Adding multiple people approaching the same target requires selecting the minimum distance among all.
  • Changing the number line to a 2D grid requires Euclidean distance comparison.
  • Introducing variable speeds adds a weighting factor to distance calculations.

FAQ

What is the main pattern used in Find Closest Person?

The main pattern is comparing absolute distances on a number line to determine who reaches the target first.

How do I handle ties when both people reach at the same time?

Return 0 whenever the distances from Person 1 and Person 2 to Person 3 are equal.

Can this approach scale to more people?

Yes, compute all distances and select the person with the smallest distance to the target.

Is simulation needed to solve this problem?

No, simple math comparison of distances is sufficient and more efficient than simulation.

What is the time complexity of this math-driven solution?

The solution runs in O(1) time since it only involves a few arithmetic operations.

terminal

Solution

Solution 1: Mathematics

We calculate the distance $a$ between the 1st person and the 3rd person, and the distance $b$ between the 2nd person and the 3rd person.

1
2
3
4
5
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)
Find Closest Person Solution: Math-driven solution strategy | LeetCode #3516 Easy