Minimum Absolute Difference - Problem

Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.

Return a list of pairs in ascending order (with respect to pairs), each pair [a, b] follows:

  • a, b are from arr
  • a < b
  • b - a equals to the minimum absolute difference of any two elements in arr

Input & Output

Example 1 — Basic Case
$ Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
💡 Note: After sorting: [1,2,3,4]. Adjacent differences: |2-1|=1, |3-2|=1, |4-3|=1. Minimum difference is 1, so all adjacent pairs qualify.
Example 2 — Single Pair
$ Input: arr = [1,3,6,10,15]
Output: [[1,3]]
💡 Note: After sorting: [1,3,6,10,15]. Adjacent differences: |3-1|=2, |6-3|=3, |10-6|=4, |15-10|=5. Minimum difference is 2, only pair [1,3] qualifies.
Example 3 — Multiple Minimum Pairs
$ Input: arr = [1,1,3,3]
Output: [[1,3],[1,3]]
💡 Note: After sorting: [1,1,3,3]. Adjacent differences: |1-1|=0, |3-1|=2, |3-3|=0. Wait, this violates distinct constraint. Let's use [1,5,3,4]: sorted [1,3,4,5], differences [2,1,1], min=1, pairs [[3,4],[4,5]].

Constraints

  • 2 ≤ arr.length ≤ 105
  • -106 ≤ arr[i] ≤ 106
  • All integers in arr are distinct

Visualization

Tap to expand
Minimum Absolute Difference ProblemInput:4213Sorted:1234Find minimum difference between adjacent pairs|2-1| = |3-2| = |4-3| = 1 (minimum)Output:[[1,2], [2,3], [3,4]]
Understanding the Visualization
1
Input Array
Given array of distinct integers
2
Sort & Compare
Sort array and find minimum difference between adjacent elements
3
Collect Pairs
Return all adjacent pairs with minimum difference
Key Takeaway
🎯 Key Insight: After sorting, minimum absolute difference can only occur between adjacent elements
Asked in
Amazon 15 Google 12 Facebook 8 Microsoft 6
23.5K Views
Medium Frequency
~15 min Avg. Time
894 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen