Code for studying algorithm