Find Boundary Indices in Sorted Sequence
Heaps, Sorting & SearchAuto-graded · 20 test casesLast updated
Adapt binary search to locate the starting and ending index of a target value in a sorted sequence in logarithmic time.
Given a non-decreasingly sorted array of integers nums and a search value target, return a pair of integers representing the inclusive starting and ending index of target in the array. If target does not appear in nums, return [-1, -1].
Your implementation must run in O(log n) time complexity.
Constraints
0 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9numsis sorted in non-decreasing order.-10^9 <= target <= 10^9
Examples
Example 1
Input: search_range([5, 7, 7, 8, 8, 10], 8)
Output: [3, 4]
Example 2
Input: search_range([5, 7, 7, 8, 8, 10], 6)
Output: [-1, -1]
Hints
Hint 1
Standard binary search terminates upon finding any matching index, which could be in the middle of a duplicated sequence. Linear scanning from that point could take O(n) time.
Hint 2
Instead, implement a binary search variant that finds the insertion point (lower bound): the first index where the element is greater than or equal to a given value.
Hint 3
You can find the starting index by searching for `target`. You can find the ending index by searching for `target + 1` and taking the index immediately preceding it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When an array contains repeated instances of a value, a standard binary search returns an arbitrary matching position. Expanding outward linearly from that position can take $O(n)$ time in the worst case (for instance, when all elements are identical to the target), violating the required logarithmic time bound.
To strictly adhere to $O(\log n)$ runtime, we can formulate a helper function lower_bound(value) that performs binary search to find the smallest index i such that nums[i] >= value. If no element satisfies this condition, it returns len(nums).
With lower_bound defined:
- Starting index:
start = lower_bound(target). Ifstart == len(nums)ornums[start] != target, the value is absent from the array, so we return[-1, -1]. - Ending index:
end = lower_bound(target + 1) - 1. The first position holding a value greater than or equal totarget + 1is equivalent to the first position strictly greater thantarget. The element immediately before it is the last occurrence oftarget.
Complexity
- Time Complexity: $\mathcal{O}(\log n)$, since we execute at most two standard binary searches, each taking $\mathcal{O}(\log n)$ steps.
- Space Complexity: $\mathcal{O}(1)$, as the search requires only a constant amount of auxiliary memory.
Python Reference Solution
from typing import List
def search_range(nums: List[int], target: int) -> List[int]:
def lower_bound(value: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < value:
lo = mid + 1
else:
hi = mid
return lo
start = lower_bound(target)
if start == len(nums) or nums[start] != target:
return [-1, -1]
end = lower_bound(target + 1) - 1
return [start, end]