left
to 0
and right
to arr.length - 1
.answer
to -1
to indicate that no fixed point has been found yet.left <= right
, do the following:
a. Calculate mid
as (left + right) / 2
.
b. If arr[mid] == mid
, update answer
to mid
and set right
to mid - 1
to search for a smaller index.
c. If arr[mid] < mid
, set left
to mid + 1
.
d. If arr[mid] > mid
, set right
to mid - 1
.answer
.