lo
to 1 and hi
to m * n
as the range of possible values for the kth smallest number.lo
is less than hi
:
a. Calculate mid
as the average of lo
and hi
.
b. Define enough(x)
to count the number of values less than or equal to mid
in the table.
c. For each row i
from 1 to m
, add min(mid // i, n)
to the count.
d. If the count is at least k
, set hi
to mid
.
e. Otherwise, set lo
to mid + 1
.hi
as the kth smallest number.