Pregunta de entrevista de Tata Consultancy Services

Onspot Coding question was given and i have to provide code for the problem statement