improve brute force solution