Pregunta de entrevista de Amazon

A puzzle which had to be solved using an algorithm and also write code for the same