solution 3 for the first problem