Skip to main content

Find Closest Person - Solution & Explanation

EasyMath9 min readAsked at: Amazon, Meta, Google +1
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: Two people stand at positions x and y on a number line. A third position z is the target. The task is to determine which person is closer to z. If both are equally close, return 0; otherwise return the index of the closer person.

Approach 1: Step-by-Step Simulation (O(d) time, O(1) space)

A straightforward way is to simulate both people moving one step at a time toward the target position z. Each step reduces the distance between the person and the target until one reaches it first. If both reach the same step count, the result is a tie. This approach models the movement literally but performs unnecessary iterations proportional to the distance d from each person to z. Since the number line distance could be large, repeatedly decrementing or incrementing values is inefficient for a problem that only requires comparing distances.

Approach 2: Absolute Distance Comparison (O(1) time, O(1) space)

The optimal solution comes directly from math. The distance between two points on a number line is the absolute difference: |a - b|. Compute d1 = |x - z| and d2 = |y - z|. If d1 < d2, person 1 is closer. If d2 < d1, person 2 is closer. If the distances are equal, both reach the target at the same time and the result is 0.

This works because movement on a number line is symmetric. Whether someone approaches from the left or right, the minimum number of steps required to reach z equals the absolute difference. The comparison therefore reduces the entire problem to two constant-time arithmetic operations and a conditional check.

The algorithm performs a couple of integer subtractions and calls to abs(), so the runtime stays constant regardless of input values. Memory usage is also constant because only a few variables are stored.

Problems like this appear frequently in beginner algorithm rounds because they test your understanding of mathematical reasoning and simple implementation. Recognizing that the problem reduces to comparing absolute distances avoids unnecessary simulation or loops.

Recommended for interviews: Use the absolute distance comparison. Interviewers expect you to recognize that distance on a number line is |a - b|. Mentioning the simulation idea briefly shows basic reasoning, but the constant-time mathematical comparison demonstrates clean problem reduction and strong fundamentals.

Solution

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.

  • If a = b, it means both people arrive at the same time, return 0;
  • If a \lt b, it means the 1st person will arrive first, return 1;
  • Otherwise, it means the 2nd person will arrive first, return 2.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Step-by-Step SimulationO(d)O(1)Conceptual understanding of movement toward a target on a number line
Absolute Distance Comparison (Mathematics)O(1)O(1)Best approach for interviews and production code; directly compares |x - z| and |y - z|

Video Solution

3516. Find Closest Person (Leetcode Easy) • Programming Live with Larry • 998 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Closest Person easy or hard?
Find Closest Person is categorized as an Easy problem. The core idea is recognizing that distance on a number line equals the absolute difference between two values, leading to a simple O(1) comparison.
Find Closest Person Python/Java solution
In Python or Java, compute Math.abs(x - z) and Math.abs(y - z) (or abs in Python) and compare the results. Return 1 if the first distance is smaller, 2 if the second is smaller, or 0 if both distances are equal.
How to solve Find Closest Person in O(1)?
Calculate the distance from each person to the target using absolute difference: d1 = |x - z| and d2 = |y - z|. If d1 < d2 return 1, if d2 < d1 return 2, otherwise return 0 for a tie. This constant-time comparison directly determines the closest person.
What is the best approach for Find Closest Person?
The best approach is comparing absolute distances from each person to the target position. Compute |x - z| and |y - z|, then return the person with the smaller value. This mathematical comparison runs in O(1) time and O(1) space and avoids unnecessary simulation.
Is Find Closest Person asked at Google/Amazon/Meta?
Problems like Find Closest Person commonly appear in screening rounds and practice sets because they test basic mathematical reasoning and implementation. While not tied to a specific company question bank, similar distance comparison problems appear in interviews at large tech companies.
What data structure is used in Find Closest Person?
No complex data structure is required. The solution relies purely on arithmetic operations and absolute value calculations from basic mathematics, making it a constant-space implementation.
What is the time complexity of Find Closest Person?
The optimal solution runs in O(1) time because it only performs constant arithmetic operations and comparisons. Space complexity is also O(1) since only a few integer variables are used.

Ready to solve this problem?

Practice Find Closest Person with our built-in code editor and test cases.

Practice on FleetCode