Finished problem 4 with 35 lines and O(n^2) runtime complexity.