Amazon interview question

leetcode interview - medium question