Ctrl K

Closest Value Pairs

Easy
You are given an array of distinct integers values. Find the smallest difference between any two of its elements and return every pair [a, b] with that difference, where a < b. Order the pairs by a in ascending order.
Example 1
values=[10, 4, 7, 1] → [[1, 4], [4, 7], [7, 10]]

Sorted, this is [1, 4, 7, 10]: every neighbouring gap is 3, so all three pairs qualify.

Example 2
values=[-5, 20, 0, 18] → [[18, 20]]

The smallest difference is 2, between 18 and 20.

Example 3
values=[3, -2] → [[-2, 3]]
Constraints
  • 2 ≤ len(values) ≤ 10⁵
  • -10⁶ ≤ values[i] ≤ 10⁶
  • All numbers in values are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMerge Into the Spare Tail EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyMake It to Every Meeting EasyThe Lost Ticket Easy
loading Python…
Input
values=[10, 4, 7, 1]
Expected
[[1, 4], [4, 7], [7, 10]]

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.