Problem. gas stations on a circle; station gives fuel and reaching the next one costs . Find a start from which you can complete the circuit with an initially empty tank, or report none.

Answer: a start exists iff ; one pass finds it: whenever the running tank goes negative, the next station becomes the candidate start.

int gasStart(const vector<long long>& gas, const vector<long long>& cost) {     // start index completing the circuit, or -1
    long long total = 0, tank = 0; int start = 0;
    for (int i = 0; i < sz(gas); i++) {
        total += gas[i] - cost[i]; tank += gas[i] - cost[i];
        if (tank < 0) start = i + 1, tank = 0;
    }
    return total >= 0 ? start : -1;
}