Closest Response Times: List the Tightest Increasing Pairs
Arrays & HashingAuto-graded · 21 test casesLast updated
Find the smallest absolute gap between any two readings, then list every strictly increasing pair achieving it, handling duplicates correctly. Tests sorting and adjacent-gap reasoning.
Closest Response Times
You are given a list of integer response-time readings (in milliseconds) collected from a service. Values may be negative, and the same value may appear more than once.
Let d be the smallest absolute difference between any two elements of the list, where two elements at different positions are always allowed to be compared, even if they hold equal values.
Return every pair [a, b] that satisfies both of the following:
a < b(strictly increasing), andb - a == d.
Output rules
- Return the pairs as a list of two-element lists.
- Sort the pairs by
aascending, then bybascending. - If the list has fewer than two elements, return an empty list.
- If any value is repeated, then
dis0. No strictly increasing pair can have a difference of0, so the answer is an empty list. A pair with a larger difference does not qualify just because it is the closest distinct pair.
Constraints
0 <= len(readings) <= 10^5- Each reading is an integer that fits in a 64-bit signed integer.
- The input list should not be relied upon to be sorted.
- Aim for better than quadratic time.
Examples
Example 1
Input: closest_reading_pairs([4, 2, 1, 3])
Output: [[1, 2], [2, 3], [3, 4]]
Example 2
Input: closest_reading_pairs([1, 3, 6, 10, 15])
Output: [[1, 3]]
Example 3
Input: closest_reading_pairs([1, 1, 3])
Output: []
Hints
Hint 1
Think about which elements could possibly be the closest to a given value. What happens to the answer if you first put the readings in order?
Hint 2
After sorting, the smallest difference over all pairs is always found between two neighbors. A pair that skips an element spans at least two gaps.
Hint 3
If the smallest gap is 0 there is a duplicate, and no strictly increasing pair can match it, so return an empty list. Otherwise, collect every neighboring pair whose gap equals the minimum.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort a copy of the readings. In sorted order, the smallest difference between any two elements is the smallest gap between neighboring elements. Compute that minimum gap d.
- If there are fewer than two readings, return
[]. - If
d == 0, a duplicate exists. The required difference is 0, but a pair must satisfya < b, so return[]. - Otherwise, scan the sorted list and collect each neighboring pair whose gap equals
d.
Why it is correct
The global minimum difference is an adjacent gap. For sorted values and indices i < j, the difference v[j] - v[i] is the sum of the adjacent gaps between them. Each gap is non-negative, so the sum is at least as large as any one of those gaps, and therefore at least the minimum adjacent gap. Adjacent pairs are themselves valid pairs, so the minimum is achieved by one of them.
Only adjacent pairs can match when d > 0. If d > 0, all values are distinct and every gap is at least d. A non-adjacent pair spans at least two gaps, so its difference is at least 2d, which is greater than d. Hence only adjacent pairs qualify.
Ordering and uniqueness. Because values are distinct in this case, each adjacent pair is strictly increasing and unique. Scanning left to right already produces pairs sorted by a (and b).
Complexity
- Time:
O(n log n), dominated by sorting. The gap computation and collection areO(n). - Space:
O(n)for the sorted copy and the output.
Reference solution
from typing import List
def closest_reading_pairs(readings: List[int]) -> List[List[int]]:
if len(readings) < 2:
return []
ordered = sorted(readings)
min_gap = min(ordered[i + 1] - ordered[i] for i in range(len(ordered) - 1))
if min_gap == 0:
# A duplicate exists; no strictly increasing pair has difference 0.
return []
return [[ordered[i], ordered[i + 1]]
for i in range(len(ordered) - 1)
if ordered[i + 1] - ordered[i] == min_gap]