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

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

    Find smallest and largest element in the list. The difference smallest-largest will be minimum.

    If you're looking for nonnegative difference, then this is of course at least as hard as checking if the array has two same elements. This is called element uniqueness problem and without any additional assumptions (like limiting size of integers, allowing other operations than comparison) requires >= n log n time. It is the 1-dimensional case of finding the closest pair of points.

提交回复
热议问题