2dbi

Minimum Absolute Difference

viaLeetCode

Problem Given an array of distinct integers, find all pairs with the minimum absolute difference, returned in ascending order (each pair [a, b] with a < b).

Input / Output

  • Input: int array arr.
  • Output: list of pairs achieving the global minimum difference, sorted ascending.

Constraints

  • 2 <= n <= 10^5; values across a wide range — sorting dominates.

Example

  • arr = [4,2,1,3] → [[1,2],[2,3],[3,4]] (min diff 1).

Expected approach

  • Sort the array: the minimum absolute difference must occur between adjacent elements. One pass computes the min gap; a second pass collects adjacent pairs with that gap (or single pass resetting the list when a smaller gap appears). O(n log n) time, O(1) extra space beyond output. If values are bounded and dense, a counting/bucket trick gets O(n + range).
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account