Need the solution in C++

The solution provided for the problem is in Java. Kindly provide the solution in C++. Thank you.