FunctionRacerProblemsRace

Nearest Aligned Point

EasyArraysPython, JavaScript, TypeScript, Java, C++

You are at (x, y). A point in points is aligned with you if it has the same x or the same y as you. Among the aligned points, find the one with the smallest Manhattan distance |x - px| + |y - py| and return its index in points. If several are tied, return the smallest index. If no point is aligned, return -1.

Example 1

x 3
y 4
points [[1,2],[3,1],[2,4],[2,3],[4,4]]
returns 2

Aligned: index 1 (distance 3), 2 (1), 4 (1). Smallest index with distance 1 is 2.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems