AMD Interview Question

Leetcode style question, medium difficulty.