Suppose I have an array similar to this (actual values probably will not be so predictable):
$ar[0] = 5;
$ar[1] = 10;
$ar[2] = 15;
$ar[3] = 20;
and I have $n = 8
, what would be a good way to find that $n
falls between $ar[0]
and $ar[1]
? Preferably, the solution would avoid looping through the entire array, as this will probably repeated many times over on an array of varying size.
Edit:
Yes, the array will always be sorted.