Amazon Interview Question

To create an algorithm that solves the question.