I got this question in an interview and I was not able to solve it.
You have a circular road, with N number of gas stations. You know the ammount of gas
Find the gas station with the most gas but for when the gas for the next station when added to the tank does not exceed the capacity of the tank.
Make circular list of stations. Find any station with positive value of
Excess = (gasAtStation - gasToMoveToNextStationNeeded)
This is current base.
While next station has negative Excess value, add it's gasAtStation and gasToMoveToNextStationNeeded to current base fields, and remove this station from list.
Repeat for all positive stations circularly.
When no more stations to remove:
If one or some non-negative stations remains in list - any of them is suitable as starting point.
Example:
A(-50) B(100) C(-20) D(-90) E(60) [C->B]
A(-50) B(80) D(-90) E(60) [D->B]
A(-50) B(-10) E(60) [A->E]
B(-10) E(10) [B->E]
E(0)