Find pairs with the minimum absolute difference
MicrosoftMachine Learning Engineer · Technical ScreenLast updated
This question evaluates array manipulation, sorting-based reasoning, and the ability to generate ordered value pairs that reflect minimum absolute differences while handling ordering and duplicates.
Given an integer array (not necessarily sorted), find the minimum absolute difference between any two distinct elements. Return all pairs of values that achieve this minimum difference.
Requirements:
- Each pair should be ordered as [smaller, larger].
- Return pairs in ascending order by their first element (and then second if needed).
Example: Input: [-1, -2, -4, -5, 7, 10] Output: [[-5, -4], [-2, -1]] Explanation: The minimum difference is 1, achieved by (-5, -4) and (-2, -1).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.