Finished problem 2 with 26 lines and O(n) complexity.