Quick Overview

Find every pair of distinct integers that attains the minimum absolute difference. Return smaller values first and pairs in canonical order while handling ties and negative inputs.

Return all integer pairs with the smallest absolute difference

Company: Headlands

Role: Quantitative Researcher

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given distinct integers, return every pair whose absolute difference is the smallest among all pairs. ### Function Signature ```python def closest_pairs(values: list[int]) -> list[list[int]]: ``` ### Rules Each pair contains its smaller value first. Return pairs in ascending lexicographic order. Each qualifying pair appears once. ### Constraints - `2 <= len(values) <= 100000`. All values are distinct. - For this practice version, values are between -1000000000 and 1000000000, so differences fit in signed 32-bit integers. ### Examples ```text Input: values = [4, 1, 3, 2] Output: [[1, 2], [2, 3], [3, 4]] ``` All returned pairs have difference 1. ```text Input: values = [-5, 9] Output: [[-5, 9]] ``` There is only one possible pair.

Overview: Find every pair of distinct integers that attains the minimum absolute difference. Return smaller values first and pairs in canonical order while handling ties and negative inputs.

Read the full Headlands Quantitative Researcher interview experience this question came from

Given distinct integers, return every pair whose absolute difference is the smallest among all pairs. ### Function Signature ```python def closest_pairs(values: list[int]) -> list[list[int]]: ``` ### Rules Each pair contains its smaller value first. Return pairs in ascending lexicographic order. Each qualifying pair appears once. ### Constraints - `2 <= len(values) <= 100000`. All values are distinct. - For this practice version, values are between -1000000000 and 1000000000, so differences fit in signed 32-bit integers. ### Examples ```text Input: values = [4, 1, 3, 2] Output: [[1, 2], [2, 3], [3, 4]] ``` All returned pairs have difference 1. ```text Input: values = [-5, 9] Output: [[-5, 9]] ``` There is only one possible pair.

Constraints

  • `2 <= len(values) <= 100000`. All values are distinct.
  • For this practice version, values are between -1000000000 and 1000000000, so differences fit in signed 32-bit integers.

Examples

Input: ([4, 1, 3, 2],)

Expected Output: [[1, 2], [2, 3], [3, 4]]

Explanation: Return every tied adjacent pair in ascending order.

Input: ([-5, 9],)

Expected Output: [[-5, 9]]

Explanation: A two-element input has exactly one possible pair.

Hints

  1. Consider what happens to a gap if another value lies strictly between its endpoints.
  2. Sorting the values can also give the required order for the resulting pairs.

Return all integer pairs with the smallest absolute difference

hardHeadlandsQuantitative Researcher

Given distinct integers, return every pair whose absolute difference is the smallest among all pairs.

Function Signature

def closest_pairs(values: list[int]) -> list[list[int]]:

Rules

Each pair contains its smaller value first. Return pairs in ascending lexicographic order. Each qualifying pair appears once.

Constraints

  • 2 <= len(values) <= 100000. All values are distinct.
  • For this practice version, values are between -1000000000 and 1000000000, so differences fit in signed 32-bit integers.

Examples

Input: values = [4, 1, 3, 2]
Output: [[1, 2], [2, 3], [3, 4]]

All returned pairs have difference 1.

Input: values = [-5, 9]
Output: [[-5, 9]]

There is only one possible pair.

Examples

Example 1
Input
([4, 1, 3, 2],)
Output
[[1, 2], [2, 3], [3, 4]]
Notes
Return every tied adjacent pair in ascending order.
Example 2
Input
([-5, 9],)
Output
[[-5, 9]]
Notes
A two-element input has exactly one possible pair.

Constraints

  • •`2 <= len(values) <= 100000`. All values are distinct.
  • •For this practice version, values are between -1000000000 and 1000000000, so differences fit in signed 32-bit integers.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def closest_pairs(values: list[int]) -> list[list[int]]:
Sample call
closest_pairs([4, 1, 3, 2])
Language
Case 1
Input
([4, 1, 3, 2],)
Expected
[[1, 2], [2, 3], [3, 4]]
Case 2
Input
([-5, 9],)
Expected
[[-5, 9]]

Browse More Questions

Show the approach

Approach

Sort the values in ascending order. If a pair skips an intermediate value, its positive gap equals the sum of at least two positive adjacent gaps. It is larger than each of those individual gaps and therefore cannot be globally closest. Scan adjacent pairs and retain those matching the best gap. Clear the previous pairs whenever a smaller gap appears. Increasing first endpoints produce the required lexicographic order directly.

Time complexity:
O(n log n)
Space complexity:
O(n) including a sorted copy and the output