An alternate solution would be to check for points at each radius going outward, starting from the origin. This would be more efficient in a scenario where there are millions of points, and highly clustered data (star maps for example).

4 Answers

▲

0

▼

An alternate solution would be to check for points at each radius going outward, starting from the origin. This would be more efficient in a scenario where there are millions of points, and highly clustered data (star maps for example).

▲

0

▼

Adding all elements first is a terrible method because if n is very large the sort will take a long time. You can store the first n integers in a max heap. Then in O(1) time check if each of the remaining elements is less than the max value in the heap. If it is, insert it in the heap, removing the max value. If it isn't move on.

▲

0

▼

Also, if you havent sorted the points, how are you supposed to check in outward radius?

To comment on this, Sign In or Sign Up.

Would you like us to review something? Please describe the problem with this {0} and we will look into it.

Your feedback has been sent to the team and we'll look into it.

Step 1: Design a class called point which has three parameters

int x

int y

int distance

Step 2: For all the points given, find the distance between them and origin

Step 3: Store the values in a binary tree

Step 4: Heap sort

and print the first n values from the binary tree