Locate Gap in Sorted Consecutive Sequence
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Detect a missing entry in a sorted contiguous array of integers in logarithmic time using binary search.
Given an array a of distinct, strictly increasing integers, the elements are expected to form an uninterrupted consecutive sequence starting from a[0].
At most one element within the range of the sequence is missing from a.
Write a function that identifies and returns the missing integer. If all elements in a are strictly consecutive and no internal gap exists, return None.
Constraints
0 <= len(a) <= 10^5-10^9 <= a[i] <= 10^9ais strictly increasing (a[i] < a[i+1]for all validi).- Time complexity must be strictly $O(\log n)$ where $n = \text{len}(a)$.
- Auxiliary space complexity must be $O(1)$.
Examples
Example 1
Input: find_missing_value([4, 5, 6, 8, 9])
Output: 7
Example 2
Input: find_missing_value([-3, -2, -1, 0, 1])
Output: None
Example 3
Input: find_missing_value([10, 11, 12, 13, 15])
Output: 14
Hints
Hint 1
Notice that if no numbers were missing, every index `i` would satisfy `a[i] == a[0] + i`.
Hint 2
When a number is missing, `a[i] == a[0] + i` holds for all elements before the gap, but fails for all elements after the gap.
Hint 3
Since the condition changes from True to False at most once, you can use binary search to locate the first index where `a[i] != a[0] + i`.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A linear search checking if a[i] != a[i-1] + 1 takes $O(n)$ time. To solve this in $O(\log n)$ time, we can leverage the array's sorted property using binary search.
Let start = a[0]. In a complete consecutive sequence without any missing numbers, each element at index i must equal start + i.
- For indices before the missing value:
a[i] == start + ievaluates toTrue. - For indices after the missing value:
a[i] == start + i + 1(or greater), soa[i] == start + ievaluates toFalse.
Because the array is sorted and distinct, this boolean condition transitions from True to False at most once. We can use binary search to locate the first index i where a[i] != start + i.
- Maintain search boundaries
[lo, hi). - Compute
mid = (lo + hi) // 2. - If
a[mid] == start + mid, the missing number (if any) lies strictly to the right, so updatelo = mid + 1. - Otherwise, the gap is at or before
mid, so updatehi = mid. - After the loop terminates, if
lo == len(a), no missing element was found in the range (returnNone). Otherwise, the missing number isstart + lo.
Complexity
- Time Complexity: $O(\log n)$ — Each step halves the remaining search range.
- Space Complexity: $O(1)$ — Only a few pointer variables are used.
Python Solution
from typing import List, Optional
def find_missing_value(a: List[int]) -> Optional[int]:
if not a:
return None
start = a[0]
lo, hi = 0, len(a)
while lo < hi:
mid = (lo + hi) // 2
if a[mid] == start + mid:
lo = mid + 1
else:
hi = mid
if lo == len(a):
return None
return start + lo