left
to 0 and right
to the length of the array minus 1.left
is less than or equal to right
:
a. Calculate the middle index mid
as the average of left
and right
(avoiding integer overflow).
b. If nums[mid]
is equal to the target, return mid
as the target is found.
c. If nums[mid]
is less than the target, move the left
pointer to mid + 1
.
d. If nums[mid]
is greater than the target, move the right
pointer to mid - 1
.left
as the insert position.