Browse curriculum

Gas Station

Find the starting gas station to complete the circuit.

Examples

Input:gas = [1,2,3,4,5], cost = [3,4,5,1,2]
Output:3
Explain:

Stations 0, 1 and 2 each run dry at once; from station 3 the tank never drops below 0 on the full lap.

Input:gas = [2,3,4], cost = [3,4,3]
Output:-1
Explain:

The route offers 9 gas but burns 10, so no start can finish.

Input:gas = [5,1,2,3,4], cost = [4,4,1,5,1]
Output:4
Explain:

Two stretches run dry, and the lap from the last station wraps past the end.

Problem Understanding

There are n gas stations on a circular route. Station i gives gas[i] units of gas, and driving from station i to the next one (from the last station back to station 0) burns cost[i].

The car starts with an empty tank at a station of your choosing. Return the index of that station if the car can drive round the whole circuit once, otherwise return -1. If a solution exists, it is guaranteed to be unique.

Strategy

One pass, adding each station’s net = gas[i] - cost[i] to two sums:

  1. total_tank, the whole trip. If it ends below 0, the route burns more than it offers and no start can finish: return -1.
  2. curr_tank, the tank since the candidate start. If it drops below 0 at station i, no station from start to i can be the start, so set start = i + 1 and empty the tank.

If total_tank ends at 0 or more, start is the answer.

Interactive Visualization

The route drawn as the loop it is. Above each station is the gas it gives; on each arrow, what that leg burns. The candidate start is outlined and the legs driven since it are lit. When the tank runs dry the failing leg turns red and the stretch is greyed out — none of those stations can be the start.

Try Late start: two stretches run dry, the answer is the last station, and the lap it completes wraps past the end.

Dry Run

gas [1, 2, 3, 4, 5], cost [3, 4, 5, 1, 2]:

i gas - cost net whole trip tank since start action
0 1 - 3 -2 -2 -2 ran dry: start = 1, tank = 0
1 2 - 4 -2 -4 -2 ran dry: start = 2, tank = 0
2 3 - 5 -2 -6 -2 ran dry: start = 3, tank = 0
3 4 - 1 3 -3 3 keep going
4 5 - 2 3 0 6 keep going

The whole trip balances at 0, which is not below 0 — return 3.

Edge Cases & Common Mistakes

Check these conditions:

  • Not enough gas overall means -1, whatever the order. If sum(gas) < sum(cost), every start runs dry somewhere. The whole-trip balance makes that check in the same pass.

  • A dry tank rules out the whole stretch, not just its start. Every station from the start to the failing leg was reached with fuel to spare, so starting at one of them can only arrive with less.

  • No second lap is needed. When the whole-trip balance ends at 0 or more, the fuel banked from the start onward covers the stations before it.

  • More than one valid start? LeetCode guarantees at most one. On an input where several work, this returns the first — every earlier station sits in a stretch that ran dry.

Solution

One pass, two running tanks — the lines light up as the figure above steps.

Gas Station Code
1var canCompleteCircuit = function(gas, cost) {
2 let totalTank = 0;
3 let currTank = 0;
4 let start = 0;
5 for (let i = 0; i < gas.length; i++) {
6 const net = gas[i] - cost[i];
7 totalTank += net;
8 currTank += net;
9 if (currTank < 0) {
10 start = i + 1;
11 currTank = 0;
12 }
13 }
14 return totalTank >= 0 ? start : -1;
15};

The approach, step by step

  1. Keep two tanks

    Add each station’s gas minus cost to a whole-trip balance and to a tank measured since the candidate start.

  2. Skip past a dry stretch

    When the tank since the start drops below 0, move the start to the next station and empty the tank.

  3. Check the whole trip

    After one pass, return the start if the whole-trip balance is at least 0, and -1 otherwise.

Frequently asked questions

Why can every station in a stretch that ran dry be skipped?

Each station between the start and the failing leg was reached with a tank of at least 0. Starting at one of them instead means arriving there with exactly 0 — never more — so the car runs dry on the same leg or sooner. None of them can be the answer, so the next candidate is the station after the failure.


Why is no second lap needed to check the answer?

The drive from the final start to the end of the array never goes below 0, and the stretches before the start add up to the whole-trip balance minus that tank. If the whole-trip balance is at least 0, the fuel banked from the start onward covers that deficit, so the wrap past the end back to the start always succeeds.


When does Gas Station return -1?

Exactly when the route offers less gas in total than it burns, that is when sum(gas) < sum(cost). The whole-trip balance is that check, kept in the same pass. If the totals balance or better, a start always exists.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description