While binary search is a pretty algorithm, I've often found myself struggling with "off-by-1" issues for certain applications of binary search.
I have seen variants of binary search where it looks like one of the following 2:
while(lo <= hi)
{
// do stuff
}
or
while(lo < hi)
{
// do stuff
}
My impression has always been that you can use either, but the body of the while
loop may change depending on which you use. Is this the correct interpretation?