Uncovered Integer Gaps Within a Closed Window
Arrays & HashingAuto-graded · 26 test casesLast updated
Scan a sorted list of distinct integers once to report every maximal run of integers in a closed window that the list skips, formatting single gaps as one-element lists.
You are given a sorted list values of distinct integers and two integers low and high. Together, low and high define the closed window of integers from low to high, including both endpoints.
Find every maximal run of consecutive integers inside the window that is not present in values. A run is maximal if it cannot be extended by another missing integer on either side while staying inside the window.
Entries of values that lie outside the window play no role and must be ignored.
Output format
Return a list of runs in ascending order. Write each run as:
[x]when the run consists of exactly one integerx[start, end]when the run contains two or more integers, wherestart < end
If nothing in the window is missing, return an empty list. If low > high, the window is empty and the result is an empty list.
Constraints
valuesis sorted in ascending order and contains no duplicates.valuesmay be empty, and may contain negative numbers.- Entries of
valuesmay be smaller thanlowor larger thanhigh. - The bounds
lowandhighmay be very far apart, so the solution should not iterate over every integer in the window. - A single-integer run must be written as
[x], never[x, x].
Examples
Example 1
Input: find_uncovered_gaps([3, 5, 10], 0, 9)
Output: [[0, 2], [4], [6, 9]]
Hints
Hint 1
Keep one cursor holding the smallest integer in the window that has not yet been accounted for; start it at `low`.
Hint 2
For each in-window value `x`, if `x` is larger than the cursor, everything from the cursor up to `x - 1` is missing. Then move the cursor to `x + 1`.
Hint 3
Skip values below `low`, stop at the first value above `high`, and after the loop report whatever remains from the cursor to `high`. Remember to use the one-element format when a gap has length one.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Walk through values once while maintaining a cursor: the smallest integer in the window that is not yet covered, either by a value from the list or by a gap already reported. It starts at low.
For each value x:
- If
x < low, skip it. - If
x > high, stop. The list is sorted, so everything after it is also too large. - If
x > cursor, then every integer fromcursortox - 1is missing. Report it as[cursor]ifcursor == x - 1, otherwise as[cursor, x - 1]. - Set
cursor = x + 1, sincexis present.
After the loop, if cursor <= high, the integers from cursor to high are missing, so report that final gap using the same single-versus-range formatting.
If low > high, return [] immediately.
Why it is correct
Invariant: before processing each in-window value, every integer in [low, cursor - 1] is either present in values or lies in a gap already reported, and nothing at or above cursor has been reported.
Because the list is sorted and distinct, no in-window value lies strictly between the previous in-window value and x. So [cursor, x - 1] contains no element of values and is entirely missing. It is maximal because the integer before it is either outside the window or a present value, and the integer after it, x, is present. Setting cursor = x + 1 restores the invariant.
When x == cursor, no gap exists and nothing is reported. After the last in-window value, [cursor, high] has no present values and is bounded by the window edge, so it is maximal as well. Gaps are produced left to right, so the output is already in ascending order.
Edge cases follow naturally: an empty list or a list entirely outside the window yields the whole window as one gap ([low] if low == high); a list that fills the window pushes cursor to high + 1, so the result is empty.
Complexity
- Time: O(n), each element is looked at at most once.
- Space: O(1) extra beyond the output, which may contain up to n + 1 gaps.
Reference solution
from typing import List
def find_uncovered_gaps(values: List[int], low: int, high: int) -> List[List[int]]:
if low > high:
return []
result = []
cursor = low # smallest integer in the window not yet accounted for
for x in values:
if x < low:
continue
if x > high:
break
if x > cursor:
if x - 1 == cursor:
result.append([cursor])
else:
result.append([cursor, x - 1])
cursor = x + 1
if cursor <= high:
if cursor == high:
result.append([cursor])
else:
result.append([cursor, high])
return result