algorithmsortingdata-structures

Is there an algorithm to find the closest element to X in an unsorted array in Ω(logN)?


A[i] is the closest element to X if there is no A[j] such that |X-A[j]| < |X-A[i]|.

Well my book says there is an algorithm that could do it because the lower bound of the decision tree is of Ω(logN), but I'm not quite sure how you can find this element without going through all N elements considering that the array is not necessarily sorted.


Solution

  • This may be a trick question. Big-Omega notation is a lower bound. An algorithm that runs in Ω(logN) time takes at least that much time, asymptotically. A straightforward linear search solves this problem in Ω(logN) time.

    If you want to solve this problem in O(logN) time, where logN is an asymptotic upper bound, then no, that's impossible.