Start learning free

LESSON 305 OF 667 · SORTING

Minimum Absolute Difference

Practice O(n log n)

You get a vector of integers nums.

Find the smallest gap between two of the numbers. Return every pair that is exactly that far apart.

Write each pair as [a, b] with a smaller than b. List the pairs in order of a, smallest first.


Example 1:

Input: nums = [20,5,8,17,11]
Output: [[5,8],[8,11],[17,20]]
Explanation: The smallest gap is 3:
8 - 5 = 3,
11 - 8 = 3,
20 - 17 = 3.

Example 2:

Input: nums = [-6,0,15,-2]
Output: [[-2,0]]
Explanation: The smallest gap is 2.
Only -2 and 0 are 2 apart.

Example 3:

Input: nums = [12,-3,7,2]
Output: [[-3,2],[2,7],[7,12]]
Explanation: The smallest gap is 5:
2 - (-3) = 5,
7 - 2 = 5,
12 - 7 = 5.

Constraints:

  • 2 to 30,002 numbers.
  • All the numbers are different.
  • Each number fits in a 32-bit signed integer.
  • The gap between any two numbers fits in a 32-bit signed integer too.

STEP 1 OF 9
    Ctrl + Enter to check