Nagarro interview question

Optimization of the code.