Is it possible to find two numbers whose difference is minimum in O(n) time

后端 未结 8 697
日久生厌
日久生厌 2020-12-04 20:05

Given an unsorted integer array, and without making any assumptions on the numbers in the array:
Is it possible to find two numbers whose difference is minimum i

8条回答
  •  醉梦人生
    2020-12-04 20:42

    I think the answer is no and the proof is similar to the proof that you can not sort faster than n lg n: you have to compare all of the elements, i.e create a comparison tree, which implies omega(n lg n) algorithm.

    EDIT. OK, if you really want to argue, then the question does not say whether it should be a Turing machine or not. With quantum computers, you can do it in linear time :)

提交回复
热议问题