Amazon interview question

leetcode looking question but slightly harder