C++ version of Naive Solution