Amazon interview question

very difficult questions in algorithms -2