Added improved algorithm for problem5