A worst case n^2 solution