Show problems as
Classic problem #1200EasyArraySorting

Minimum Absolute Difference

Real-world version: Tightest Gauge Gaps
Tracked in this browser only
Read Minimum Absolute Difference on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A calibration bench measures a batch of gauges against a reference block and records readings, the signed offset in micrometres that each gauge showed. The readings come in the order the gauges were measured, which is not necessarily sorted, and no two gauges showed the same offset.

Call the tightest gap the smallest difference between two of the recorded offsets.

Return every pair of offsets whose difference equals the tightest gap. Write each pair as [lower, higher] and list the pairs by their lower offset, smallest first. No two reported pairs share a lower offset, so that order is unambiguous.

Examples

Example 1

Input
readings = [3, 8, 1, 4, 9]
Output
[[3, 4], [8, 9]]

The tightest gap is 1 micrometre, and two pairs of offsets sit that close: 3 with 4, and 8 with 9. The pair with the lower first offset is listed first.

Example 2

Input
readings = [-7, -2, 0, -9]
Output
[[-9, -7], [-2, 0]]

The tightest gap is 2 micrometres, reached by -9 with -7 and by -2 with 0.

Example 3

Input
readings = [5, -5]
Output
[[-5, 5]]

Two gauges leave a single pair, whose difference of 10 micrometres is therefore the tightest gap.

Constraints

  • 2 <= readings.length <= 10^5
  • -10^6 <= readings[i] <= 10^6
  • All offsets in readings are distinct

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def tightest_gaps(readings: list[int]) -> list[list[int]]:
Java
public List<List<Integer>> tightestGaps(int[] readings)