Amazon Interview Question

Please solve an algorithm related problem