Amazon Interview Question

Rotten oranges.Refer geeksforgeeks for the solution