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

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

    The best I can think of is to counting sort the array (possibly combining equal values) and then do the sorted comparisons -- bin sort is O(n + M) (M being the number of distinct values). This has a heavy memory requirement, however. Some form of bucket or radix sort would be intermediate in time and more efficient in space.

提交回复
热议问题