Pregunta de entrevista de AMD

Leetcode style question, medium difficulty.