AlgoMaster Logo

Gas Station

mediumFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We have gas stations arranged in a circle. At each station, we pick up some gas and spend some gas to reach the next station. We need to find a starting station where we can complete the full loop without running out of gas at any point.

Two things make this interesting. First, the route is circular, so after station n-1 we wrap back to station 0. Second, we start with an empty tank, so the very first station we visit must give us enough gas to at least reach the next one.

At each station, what matters is the net gain: gas[i] - cost[i]. If the net gain is positive, we gain fuel at that station. If it's negative, we lose fuel. The whole problem reduces to one question: can we find a starting point where the running sum of net gains never drops below zero?

Key Constraints:

  • 1 <= n <= 10^5 -- With up to 100,000 stations, an O(n^2) brute force that tries every starting point and simulates the full trip would be 10^10 operations, which is too slow. We need O(n) or O(n log n).
  • 0 <= gas[i], cost[i] <= 10^4 -- Values are non-negative, so no tricky negative gas amounts. The net gain at each station ranges from -10^4 to +10^4.
  • The solution is guaranteed to be unique if it exists, so the greedy approach below never has to break ties or choose between multiple valid answers.

Approach 1: Brute Force

Intuition

Try every station as a potential starting point. For each candidate, simulate the full trip around the circuit and check if the tank ever drops below zero.

At station i, we pick up gas[i] and spend cost[i] to drive to the next station. If at any point the tank goes negative, this starting point fails and we move on to the next one.

Algorithm

  1. For each station i from 0 to n - 1:
    • Set tank = 0.
    • Starting at station i, visit all n stations in circular order.
    • At each station j, add gas[j] and subtract cost[j].
    • If tank drops below zero at any point, break and try the next starting station.
    • If we complete all n stations without the tank going negative, return i.
  2. If no starting station works, return -1.

Example Walkthrough

1Try start=0: tank=0, gain=-2, tank=-2 < 0, fail
0
start
-2
1
-2
2
-2
3
3
4
3
1/5

Code

This is too slow for n up to 10^5. When we start at station i and fail at station j, we discard everything we learned and start over at i+1. The next approach uses that failure to skip ahead instead.

Approach 2: Greedy (Single Pass)

Intuition

One observation collapses the problem into a single pass. Start at station i and drive forward. If the tank drops below zero when we arrive at station j, then no station between i and j can be a valid start either.

Here is why. Starting at i, when we reached any intermediate station k between i and j, the tank was non-negative (otherwise we would have failed earlier at k). So station k was reached with some leftover gas carried from stations i through k-1. If even that leftover gas couldn't carry us past j, then starting at k with an empty tank cannot either.

So when the tank goes negative at j, we skip every station from i to j and try j + 1 as the next candidate. That turns the O(n^2) brute force into O(n).

The algorithm tracks two values at once: a totalGain that sums every net gain to decide whether any solution exists, and a currentTank with a start pointer that locates the actual starting station using the skip-ahead rule.

Algorithm

  1. Initialize totalGain = 0, currentTank = 0, and start = 0.
  2. For each station i from 0 to n - 1:
    • Compute the net gain: gain = gas[i] - cost[i].
    • Add gain to both totalGain and currentTank.
    • If currentTank < 0, the current candidate start can't work. Set start = i + 1 and reset currentTank = 0.
  3. After the loop, if totalGain >= 0, return start. Otherwise return -1.

Example Walkthrough

1Initialize: totalGain=0, currentTank=0, start=0
0
start
-2
i
1
-2
2
-2
3
3
4
3
1/6

Code