code for coursera Algorithm