time : O(logn) space: O(1);