Solution for problem2