lo
to 0 and hi
to 1, as the smallest fraction is greater than 0 and the largest is less than or equal to 1.lo
is less than hi
, do the following:
a. Calculate mi
as the average of lo
and hi
.
b. Initialize count
to 0 and maxFraction
to (0, 1).
c. For each j
from 1 to len(arr) - 1
, find the largest i
such that arr[i] / arr[j] < mi
using a linear scan.
d. Update count
by adding i
and update maxFraction
if arr[i] / arr[j]
is greater than the current maxFraction
.
e. If count
is less than k
, set lo
to mi
, otherwise set hi
to mi
.maxFraction
as the answer.