GeeksforGeeks » Algorithms

Find a pair of close numbers

(3 posts)
  1. raghu
    guest
    Posted 1 year ago #

    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.

  2. Anil Gowda
    Member
    Posted 1 year ago #

    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....

  3. kewl_coder
    Member
    Posted 1 year ago #

    @anil24g :my friend you have to do it in o(n) time


Reply

You must log in to post.

RSS feed for this topic