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