GeeksforGeeks » Algorithms
Find a pair of close numbers
(3 posts)-
For a set S of n real numbers, a pair of elements x, y belong to S, where x < y, are said to be close if
y – x <= ( max(S) – min(S) ) / (n-1)
Suppose you are given an unsorted array A[1 : n] of distinct real numbers. Design an algorithm that finds a pair of close numbers in A in O(n) time. -
first sort the array in non decreasing order
then,check the difference between the sorted elements and keep the track of variables compared
then select the one with lowest diff.... -
@anil24g :my friend you have to do it in o(n) time
Reply
You must log in to post.