Pergunta de entrevista da empresa Tata Consultancy Services

The algorithm I used in my code.