Amazon Interview Question

How do you solve the difficulty