JuniorMath

Find Closest Person

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
Inputx = 2, y = 7, z = 4
Output1
Person 1 can reach Person 3 in 2 steps, while Person 2 needs 3 steps, so Person 1 arrives first.
Example 2
Inputx = 2, y = 5, z = 6
Output2
Person 1 needs 4 steps to reach Person 3, while Person 2 needs 1 step, so Person 2 arrives first.

Constraints

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

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate