Time: O(logN), Space: O(1)