Loading content...
You are given an array of integers nums that is sorted in ascending (non-decreasing) order, along with an integer target. Your task is to determine the starting index and ending index of the range where target appears in the array.
If the target value does not exist anywhere in the array, return [-1, -1].
Critical Requirement: Your solution must achieve a time complexity of O(log n), where n is the length of the array. A linear scan solution, while correct, will not be considered optimal for this problem.
This problem tests your ability to leverage the sorted nature of the input to perform efficient range queries using binary search techniques. Since the array may contain duplicate values, you must carefully identify both the leftmost (first) and rightmost (last) occurrences of the target.
nums = [5,7,7,8,8,10]
target = 8[3,4]The value 8 first appears at index 3 and last appears at index 4. Therefore, the range is [3, 4].
nums = [5,7,7,8,8,10]
target = 6[-1,-1]The value 6 does not exist in the array, so we return [-1, -1].
nums = []
target = 0[-1,-1]The array is empty, so any target value cannot be found. Return [-1, -1].
Constraints