Facebook Pixel

Minimum Absolute Difference Pairs

Problem Description

Given an array of integers, find all pairs of adjacent elements (after sorting) that have the minimum absolute difference. Return these pairs in sorted order.

Input: An array of integers

Output: A list of pairs [a, b] where a < b and |b - a| is the minimum absolute difference in the array

Examples:

Input: [4, 2, 1, 3]
Output: [[1, 2], [2, 3], [3, 4]]
Explanation: After sorting: [1, 2, 3, 4]
             All adjacent pairs have difference 1 (the minimum)

Input: [1, 5, 3, 19, 18, 25]
Output: [[18, 19]]
Explanation: After sorting: [1, 3, 5, 18, 19, 25]
             Minimum difference is 1, only the pair (18, 19) has this difference

Input: [5, 5, 5, 5]
Output: [[5, 5], [5, 5], [5, 5]]
Explanation: After sorting: [5, 5, 5, 5]
             All adjacent pairs have difference 0 (the minimum)
Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro