a o(n^2) solution