Find all pairs of distinct integers with the minimum absolute difference

Quick Overview

Given an array of distinct integers, return every pair whose difference equals the smallest gap between any two elements, listed in ascending order. It tests sorting, reasoning about which pairs can be closest, and a linear scan after the sort.

Find all pairs of distinct integers with the minimum absolute difference

Company: Agoda

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of **distinct integers** `arr`, find all pairs of elements with the **minimum absolute difference** between them. Return a list of pairs in **ascending order** (with respect to pairs). Each pair `[a, b]` must satisfy the following conditions: - `a` and `b` are elements from `arr`. - `a < b`. - `b - a` equals the minimum absolute difference of any two elements in `arr`. ### Function Signature ```python def minimum_abs_difference(arr: list[int]) -> list[list[int]]: ``` ### Rules - `arr` is not necessarily sorted. - Each pair is written as `[smaller, larger]`. - The list of pairs is sorted ascending by the first element. Because the values are distinct, no two pairs share a first element, so this order is unique. ### Constraints - `2 <= arr.length <= 10⁵` - `-10⁶ <= arr[i] <= 10⁶` - All integers in `arr` are distinct. ### Examples ```text Input: arr = [4,2,1,3] Output: [[1,2],[2,3],[3,4]] ``` Sorted, the array is `[1,2,3,4]`. The minimum difference is 1, and each of the three neighboring pairs has that difference. ```text Input: arr = [1,3,6,10,15] Output: [[1,3]] ``` The gaps between neighbors are 2, 3, 4 and 5. The minimum is 2, so the only pair is `[1,3]`.

Overview: Given an array of distinct integers, return every pair whose difference equals the smallest gap between any two elements, listed in ascending order. It tests sorting, reasoning about which pairs can be closest, and a linear scan after the sort.

Agoda logo
Agoda
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

Return a list of pairs in ascending order (with respect to pairs). Each pair [a, b] must satisfy the following conditions:

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

Function Signature

def minimum_abs_difference(arr: list[int]) -> list[list[int]]:

Rules

  • arr is not necessarily sorted.
  • Each pair is written as [smaller, larger] .
  • The list of pairs is sorted ascending by the first element. Because the values are distinct, no two pairs share a first element, so this order is unique.

Constraints

  • 2 <= arr.length <= 10⁵
  • -10⁶ <= arr[i] <= 10⁶
  • All integers in arr are distinct.

Examples

Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]

Sorted, the array is [1,2,3,4]. The minimum difference is 1, and each of the three neighboring pairs has that difference.

Input: arr = [1,3,6,10,15]
Output: [[1,3]]

The gaps between neighbors are 2, 3, 4 and 5. The minimum is 2, so the only pair is [1,3].