add algorithm http://www.careercup.com/question?id=19300678