Optimize the algorithm using recursion