Pregunta de entrevista de Tata Consultancy Services

I was asked to write an algorithm for a problem.