better smart way for problem 5