time O(logn - n) space O(1)