Find the closest point in O(logN) complexity?
"Given a billion points p1, p2, p3,...,pN in which each point is of the form (x1,y1). Also given a point Q which is of the form (x,y). Find the closest point P from Q. Constraint: Time Complexity O(logN)".
The Naive's solution to this problem is of the order O(N). I'm wondering if there's any optimization to this to get the best case order O(logN).
EDIT: Each point {pi} is unique.
7 comments
[ 3.3 ms ] story [ 26.0 ms ] thread[a] represents the idea that there may be an arbitrary number of steps in the portion of the program that is proportional to [log n]
[c] represents the idea that there is a constant amount of running time overhead that is independent of the core efficiency of the algorithm.
Big O notation is convenient because it gets rid of [a] and [c]. It can be misleading because [a] and [c] might dominate the run time in all practical cases. It can also be misleading because as [n] becomes large, O(n log n) often approximates (a log n).
Here we are only doing one O(n log n) operation...so long as the sorted array is memoized and used for future operations. Note that if the array is sorted destructively, then the memory is [a' * n] or O(n} which is the same as having an unsorted array (though [a'] may be bigger than the factor required without sorting.
Anyway, I don't really know what is or isn't a naive solution. But I've read a bit of Knuth and I don't think he would encourage looking for unneeded complexity because algorithms and computer science are hard enough just taking the simplest approach.