Search⌘ K
AI Features

Solution: Find First and Last Position of Element in Sorted Array

Explore how to efficiently identify the first and last occurrence of a target element in a sorted array by implementing a modified binary search. This lesson guides you through developing a helper function that continues searching left and right boundaries beyond the initial match, ensuring you understand both the approach and its O(log n) runtime complexity for optimal performance.

Statement

You are given an integer array, nums, that is sorted in non-decreasing order. Your task is to find the first and last indexes of a given value, target, within this array.

If the target does not appear in the array at all, return [-1, -1].

Note: You must write an algorithm with O(logn)O(\log n) ...