solving the problem from the opposite direction