problem 36
problem 243, O(n^2 logn)