Solution for problem 24