Implemented 02 problem in C++