Amazon interview question

LeetCode question hard and easy