Amazon Interview Question

leetcode question. easy level. look for efficient way