Implementated C++ solution to problem 1.