Before stating I have put approx 1 hours to design this solution, so please read the complete article. The extra space of n size used in this approach can be avoided but I have kept it to simplify the explanation.
Firstly, we will make an array of extra fuel available at each station, i.e. gas available at that station minus gas required to move to next station and also we will do sum of these i.e total extra fuel, if total extra fuel<0 ,it means that it tis impossible to complete cycle irrespective of starting position so return -1.
After that we will start from index 0 and traverse the extra fuel array which we made earlier and update a variable total extra, to make sure that we are not stuck at any station while completing cycle, we need to make sure that this total extra variable should always be positive
If it becomes negative, we will check from next position.
Now if initially you got total_extratime>=0, it guarantees that solution is possible.
int Solution::canCompleteCircuit(const vector &A, const vector &B)
//x is extra gas at each station
Please comment if you need further explanation
Do enlighten me if you have better solution