Pergunta de entrevista da empresa IBM

Write a code for the problem (given during interview) in any language ?