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