Skip to content

3516. Find Closest Person

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.

 

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

Solutions

Solution 1: Mathematics

Thinking

The three people move at the same speed on the number line, so the first to reach \(z\) is decided by distance. Compare \(|x-z|\) and \(|y-z|\).

No timeline simulation is required; the answer is \(0\), \(1\), or \(2\) in constant time.

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)\).

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)
1
2
3
4
5
6
7
class Solution {
    public int findClosest(int x, int y, int z) {
        int a = Math.abs(x - z);
        int b = Math.abs(y - z);
        return a == b ? 0 : (a < b ? 1 : 2);
    }
}
1
2
3
4
5
6
7
8
class Solution {
public:
    int findClosest(int x, int y, int z) {
        int a = abs(x - z);
        int b = abs(y - z);
        return a == b ? 0 : (a < b ? 1 : 2);
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
func findClosest(x int, y int, z int) int {
    a, b := abs(x-z), abs(y-z)
    if a == b {
        return 0
    }
    if a < b {
        return 1
    }
    return 2
}

func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}
1
2
3
4
5
function findClosest(x: number, y: number, z: number): number {
    const a = Math.abs(x - z);
    const b = Math.abs(y - z);
    return a === b ? 0 : a < b ? 1 : 2;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl Solution {
    pub fn find_closest(x: i32, y: i32, z: i32) -> i32 {
        let a = (x - z).abs();
        let b = (y - z).abs();
        if a == b {
            0
        } else if a < b {
            1
        } else {
            2
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/**
 * @param {number} x
 * @param {number} y
 * @param {number} z
 * @return {number}
 */
var findClosest = function (x, y, z) {
    const a = Math.abs(x - z);
    const b = Math.abs(y - z);
    return a === b ? 0 : a < b ? 1 : 2;
};
1
2
3
4
5
6
7
public class Solution {
    public int FindClosest(int x, int y, int z) {
        int a = Math.Abs(x - z);
        int b = Math.Abs(y - z);
        return a == b ? 0 : (a < b ? 1 : 2);
    }
}

Comments