Added an algorithm to do this problem without using recursion.