← Back to challenges

Minimum Difference Pair

JavaScriptHardarrayslanguage_fundamentals

Instructions

Given an array of numbers, return the pair of numbers that give the minimum absolute difference. Return the pair as an array, sorted in ascending order. If multiple pairs have the same difference, return the pair with the smallest sum.

Examples

minDifferencePair([40, 16, 8, 17, 15]) ➞ [15, 16]
// [15, 16] has smaller sum than [16, 17]

minDifferencePair([1, -31, -27, -18, -48, -15, -11, -34]) ➞ [-34, -31]

minDifferencePair([0, 2, 35, 42, 45, 14, -6, -1]) ➞ [-1, 0]

minDifferencePair([32, 33, 4, 6, 48, 18, 20, -7, -4, 31]) ➞ [31, 32]

Notes

There will be no duplicate numbers in the array.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.