Minimum Absolute Difference

EasyArrays & Hashinggreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array arr of **distinct** integers. Find the smallest absolute difference between any two elements. Return every pair [a, b] with a < b whose difference b - a equals that minimum. List the pairs in **ascending order of a**.

Examples

Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
The minimum difference is 1, achieved by three pairs.
Input: arr = [1,3,6,10,15]
Output: [[1,3]]
The minimum difference is 2, only between 1 and 3.
Input: arr = [3,8,-10,23,19,-4,-14,27]
Output: [[-14,-10],[19,23],[23,27]]
The minimum difference is 4.

Constraints

  • 2 <= arr.length <= 10^5
  • -10^6 <= arr[i] <= 10^6
  • All values in `arr` are distinct

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems