Skip to content
BytePatterns

Pairs With the Smallest Gap

EasySorting#sort-first#adjacent-scan~15m

Problem

A timing service logs event timestamps and wants the pairs of events that happened closest together. Given a list of at least two distinct integers, find the smallest absolute difference between any two of them and return every pair [a, b] with a < b that has that difference, listed in ascending order of a. The list has up to 100,000 values, so checking every pair is too slow.

Examples

Input:  nums = [4, 2, 1, 3]
Output: [[1, 2], [2, 3], [3, 4]]
Why:    the smallest gap is 1, and three pairs have it
Input:  nums = [3, 8, -10, 23, 19, -4, -14, 27]
Output: [[-14, -10], [19, 23], [23, 27]]
Input:  nums = [1, 3, 6, 10, 15]
Output: [[1, 3]]
Why:    edge case, only one pair has the smallest gap

Hints

0 / 3

Stuck on the idea rather than the code? Which Sort When? covers it.