left
to 0 and right
to len(arr) - k
.left < right
:
a. Calculate mid
as (left + right) // 2
.
b. Compare arr[mid]
and arr[mid + k]
to x
.
c. If arr[mid]
is closer to x
, move right
to mid
.
d. Otherwise, move left
to mid + 1
.left
will be at the start of the k
closest elements.left
to left + k
.