solution 2 for the first problem