brute force solution of Problem 4