def search_range(nums, target):
# Function to find the first occurrence of the target
def find_first(nums, target):
left, right = 0, len(nums) - 1
first_pos = -1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] >= target:
right = mid - 1
else:
left = mid + 1
if nums[mid] == target:
first_pos = mid
return first_pos
# Function to find the last occurrence of the target
def find_last(nums, target):
left, right = 0, len(nums) - 1
last_pos = -1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] <= target:
left = mid + 1
else:
right = mid - 1
if nums[mid] == target:
last_pos = mid
return last_pos
# Return the positions of the first and last occurrences
return [find_first(nums, target), find_last(nums, target)]
# Driver code with test cases
if __name__ == "__main__":
test_cases = [
([5, 7, 7, 8, 8, 10], 8),
([5, 7, 7, 8, 8, 10], 6),
([], 0)
]
for nums, target in test_cases:
print(f"nums: {nums}, target: {target}, result: {search_range(nums, target)}")