Iterative solution

Be careful about the begin, middle and end index for binary search.