Time: O(n^2), Space: O(1)