Problem 3: complete solution.