solve Problem 3 elegantly