I'm trying to solve a problem that requires me to find a pair with minimum difference in an array.
For example, if the array is
6,7,1,3,9
The output is
(6,7)
with difference of 1 which is minimum.
The fastest solution I can come up with is to sort the array and iterate through the sorted array to find the minimum difference [O(nlogn)]. Is there a way to optimise this or better solve it in O(n) or O(logn)?
Edit: All elements of the array are distinct.