Amazon interview question

leetcode question. easy level. look for efficient way