left
to 0 and right
to the length of nums
minus 1.left
is less than right
, perform the following steps:
a. Calculate the midpoint mid
as left + (right - left) // 2
.
b. Calculate the number of missing elements to the left of mid
as nums[mid] - nums[0] - mid
.
c. If the number of missing elements is less than k
, set left
to mid + 1
.
d. Otherwise, set right
to mid
.left
will be the rightmost index with fewer missing elements than k
. Calculate and return the kth
missing number as nums[0] + left + k
.