Finished problem 3 with 24 lines and O(nlogn) runtime complexity.