There are k stars in the space and an origin star, find the nearest 100 stars from the origin star.
Answers
This problem can be solved by comparing.
Let the distance between the stars be (a,b)
Distance between 2 points can be calculated using the formula:
dist = (x1,y1) to (x2,y2) = square root((x2 - x1)^2 + (y2 - y1)^2);
Therefore O(K) time is required.
For every star ‘n’ from K ie (n-K)
You need to find distance from the origin star K, compare the distance with the previous distance you got. Every time the new star is found closer to K than the previous, replace it with the new one.
Alternative method:
-----------------------------
Answer:
This problem can be solved by comparing.
Let the distance between the stars be (a,b)
Distance between 2 points can be calculated using the formula:
dist = (x1,y1) to (x2,y2) = square root((x2 - x1)^2 + (y2 - y1)^2);
Therefore O(K) time is required.
For every star ‘n’ from K ie (n-K)
You need to find distance from the origin star K, compare the distance with the previous distance you got. Every time the new star is found closer to K than the previous, replace it with the new one.
Alternative method:
Alternative method:-----------------------------
Another method is O((n-k)logk) time is taken to complete. Similarly do for rest of the stars to find the nearest. Overall Time complexity is O(k + (n-k)logk + klogk).
Step-by-step explanation:
pray for me to come over here