Added implementation for hard portion of problem 5 in Chapter 19