code for algorithm