solved problem 10 efficiently