• 0

Closest pair of numbers

Problem description :

Given a list of unsorted integers, find the pair of elements that have the smallest absolute difference between them? If there are multiple pairs, find them all and return them in the sorted order.

Input : An array

[ 5, 4, 3, 2 ]

Output : An array

[ 2, 3, 3, 4, 4, 5 ]

Logic :

  • Sort the given array
  • Find the lowest difference for a pair of elements in the sorted array
  • Find all the pairs with the lowest difference

Solution :


Further reading