Amazon interview question

How do you solve the difficulty