Capgemini Engineering interview question

To write the algorithm