Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

N Points Near Origin (Main Thread)

Here is the interview question prompt, presented for reference.

You are given an array of points where each point represents a point on the X-Y plane points[i] = [xi, yi]. You are also given an integer n. Find the n closest points to the origin (0, 0).

Your answer should be in descending order of distance (largest to smallest). The answer is guaranteed to be unique (except for the order it is in).

The distance between two points on the X-Y plane is the Euclidean distance. It is calculated using the formula, √(x1 - x2)2 + (y1 - y2)2.

![image](https://storage.googleapis.com/algodailyrandomassets/curriculum/medium-arrays/N%20Points%20Near%20Origin/problem.png)

For example, consider two points [1, 1] and [1, 2], and n = 1. The distance between [1, 1] and the origin is sqrt(2) (approximately 1.42), and the distance between [1, 2] and the origin is sqrt(5) (approximately 2.24). sqrt(2) is the smaller of two numbers, so the point [1, 1] is closer to the origin, which makes it the answer.

Constraints

  • 1 <= n <= points.length <= 104
  • -104 < xi, yi < 104

You can see the full challenge with visuals at this link.

Challenges • Asked over 1 year ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Dec 01, 2022:

This is the main discussion thread generated for N Points Near Origin (Main Thread).