Baseline Binary Search · 1D Arrays O(log n) · O(1)
A monitoring system keeps millions of log entries sorted by an internal fault code, and an on-call engineer needs the full span of one specific code. Because the code repeats, its first and last entries must both be found. If the code never appears, the answer is an empty span.
Input: An array nums of n integers sorted in non-decreasing order, and an integer target.
Output: A two-element array [first, last] of 0-indexed positions bounding every occurrence of target, or [-1, -1] when target is absent.
0 <= n <= 10^5-10^9 <= nums[i] <= 10^9nums is sorted in non-decreasing orderAn O(log n) algorithm is requiredInput: {"nums":[5,7,7,8,8,10],"target":8}
Output: [3,4]
The code 8 occupies two adjacent entries at indices 3 and 4.
Input: {"nums":[5,7,7,8,8,10],"target":6}
Output: [-1,-1]
Code 6 never appears in the log, so both bounds report -1.