en/docs/chapter_searching/binary_search_edge.md
!!! question
Given a sorted array `nums` of length $n$ that may contain duplicate elements, return the index of the leftmost element `target` in the array. If the array does not contain the element, return $-1$.
Recall the method for finding the insertion point with binary search. After the search completes, $i$ points to the leftmost target, so finding the insertion point is essentially finding the index of the leftmost target.
Consider implementing the left boundary search using the insertion point finding function. Note that the array may not contain target, which could result in the following two cases:
nums[i] is not equal to target.When either of these situations occurs, simply return $-1$. The code is shown below:
[file]{binary_search_edge}-[class]{}-[func]{binary_search_left_edge}
So how do we find the rightmost target? The most direct approach is to modify the code and replace the pointer shrinking operation in the nums[m] == target case. The code is omitted here; interested readers can implement it themselves.
Below we introduce two more clever methods.
In fact, we can use the function for finding the leftmost element to find the rightmost element. The specific method is: Convert finding the rightmost target into finding the leftmost target + 1.
As shown in the figure below, after the search completes, pointer $i$ points to the leftmost target + 1 (if it exists), while $j$ points to the rightmost target, so we can simply return $j$.
Note that the returned insertion point is $i$, so we need to subtract $1$ from it to obtain $j$:
[file]{binary_search_edge}-[class]{}-[func]{binary_search_right_edge}
We know that when the array does not contain target, $i$ and $j$ will eventually point to the first elements greater than and less than target, respectively.
Therefore, as shown in the figure below, we can construct an element that does not exist in the array to find the left and right boundaries.
target: Can be converted to finding target - 0.5 and returning pointer $i$.target: Can be converted to finding target + 0.5 and returning pointer $j$.The code is omitted here, but the following two points are worth noting:
target in the function needs to be changed to a floating-point type (Python does not require this change).