Capgemini Engineering Interview Question

To write the algorithm