Problem 6: complete solution.