Practice algorithm in c++