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

后端 未结 8 699
日久生厌
日久生厌 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 :)

    0 讨论(0)
  • 2020-12-04 20:47

    I don't think you can to it in O(n). The best I can come up with off the top of my head is to sort them (which is O(n * log n)) and find the minimum difference of adjacent pairs in the sorted list (which adds another O(n)).

    0 讨论(0)
提交回复
热议问题