solved problem 4. not brute force.