Find upper bound and lower bound

Why on running it gaves wrong answer.

make your own upper bound and lower bound function, we are locating the leftmost (or rightmost) index containing target (rather than returning true iff we find target), the algorithm does not terminate as soon as we find a match. Instead, we continue to search until lo == hi and they contain some index at which target can be found.
see this: