Added problem 3 algorithm