Pregunta de entrevista de Amazon

leetcode question. easy level. look for efficient way