improved Problem 1 and added solutions in java and C++