Pregunta de entrevista de Amazon

optimized solution need to be derived