Problem

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

1
2
3
4
5
6
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

1
2
3
4
5
6
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

1
2
3
4
5
6
7
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

Examples

Solution

Method 1 – Absolute Distance Comparison 1

Intuition

The person who reaches Person 3 first is the one whose distance to Person 3 is smallest. If both are equally close, they arrive at the same time.

Approach

  1. Compute the absolute distance from Person 1 to Person 3: d1 = |x - z|.
  2. Compute the absolute distance from Person 2 to Person 3: d2 = |y - z|.
  3. If d1 < d2, return 1. If d2 < d1, return 2. If equal, return 0.

Code

1
2
3
4
5
6
7
8
9
class Solution {
public:
    int findClosestPerson(int x, int y, int z) {
        int d1 = abs(x - z), d2 = abs(y - z);
        if (d1 < d2) return 1;
        if (d2 < d1) return 2;
        return 0;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
func findClosestPerson(x, y, z int) int {
    d1 := abs(x - z)
    d2 := abs(y - z)
    if d1 < d2 {
        return 1
    }
    if d2 < d1 {
        return 2
    }
    return 0
}
func abs(a int) int {
    if a < 0 {
        return -a
    }
    return a
}
1
2
3
4
5
6
7
8
class Solution {
    public int findClosestPerson(int x, int y, int z) {
        int d1 = Math.abs(x - z), d2 = Math.abs(y - z);
        if (d1 < d2) return 1;
        if (d2 < d1) return 2;
        return 0;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    fun findClosestPerson(x: Int, y: Int, z: Int): Int {
        val d1 = kotlin.math.abs(x - z)
        val d2 = kotlin.math.abs(y - z)
        return when {
            d1 < d2 -> 1
            d2 < d1 -> 2
            else -> 0
        }
    }
}
1
2
3
4
5
6
7
8
9
class Solution:
    def findClosestPerson(self, x: int, y: int, z: int) -> int:
        d1 = abs(x - z)
        d2 = abs(y - z)
        if d1 < d2:
            return 1
        if d2 < d1:
            return 2
        return 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl Solution {
    pub fn find_closest_person(x: i32, y: i32, z: i32) -> i32 {
        let d1 = (x - z).abs();
        let d2 = (y - z).abs();
        if d1 < d2 {
            1
        } else if d2 < d1 {
            2
        } else {
            0
        }
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    findClosestPerson(x: number, y: number, z: number): number {
        const d1 = Math.abs(x - z);
        const d2 = Math.abs(y - z);
        if (d1 < d2) return 1;
        if (d2 < d1) return 2;
        return 0;
    }
}

Complexity

  • ⏰ Time complexity: O(1), as only a few arithmetic operations are performed.
  • 🧺 Space complexity: O(1), as only a constant amount of extra space is used.