Because you cannot have a negative 0.
Consider the situation if there was -1
. If the element was found with index 0, it will return 0. If the element was not found, but its insertion point was 0, it would also return zero. How could you distinguish between these two situations? With the addition of -1
, they now return 0
and -1
respectively, allowing you to distinguish.
And this -(insertion point) - 1
, slightly different from what your question says.
source share