Created
September 4, 2025 18:44
-
-
Save tatsuyax25/88befc924ef5c276191d7ec3fb6da3cb to your computer and use it in GitHub Desktop.
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 to
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} x | |
* @param {number} y | |
* @param {number} z | |
* @return {number} | |
*/ | |
var findClosest = function(x, y, z) { | |
// Calculate absolute distance from Person 1 to Person 3 | |
const distance1 = Math.abs(x - z); | |
// Calculate absolute distance from Person 2 to Person 3 | |
const distance2 = Math.abs(y - z); | |
// Compare distances to determine who arrives first | |
if (distance1 < distance2) { | |
return 1; // Person 1 is closer | |
} else if (distance2 < distance1) { | |
return 2; // Person 2 is closer | |
} else { | |
return 0; // Both are equidistant | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment