Domanda di colloquio di AMD

Leetcode style question, medium difficulty.