think-cell interview question

Implement the code in C++