Solution for Problem 2 fully explained