Amazon interview question

Please solve an algorithm related problem