Amazon interview question

Rotten oranges.Refer geeksforgeeks for the solution