Skip to content
AI360Xpert
Beta
Difficulty: MediumGreedy

Gas Station

Problem in Plain English

There are n gas stations in a circle, each giving some gas and charging some cost to reach the next one. Find the station to start from so the car completes the full loop, or report that it is impossible.

Problem Statement

There are n gas stations on a circular route. Station i provides gas[i] fuel; traveling clockwise to the next station costs cost[i]. After station n - 1, the next station is 0. Start with an empty, unlimited-capacity tank, collect fuel at each visited station, and pay for its outgoing leg.

Return the zero-based starting index that completes exactly one circuit without running out of fuel, or -1 if no start works. A valid starting index is guaranteed unique when a solution exists.

Constraints

  • n equals the length of both input arrays.
  • 1≤n≤1051 \le n \le 10^5.
  • 0≤gas[i],cost[i]≤1040 \le \text{gas}[i], \text{cost}[i] \le 10^4.
  • If a valid starting index exists, it is unique.

Examples

Example 1

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

Starting at 3, the tank after each outgoing leg is 3, 6, 4, 2, 0 along the station order 3, 4, 0, 1, 2. All five legs are affordable, including the last leg back to 3. Starts 0, 1, and 2 fail immediately; start 4 reaches a negative tank at station 2.

Example 2

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

The route has 9 units of fuel but costs 10 units. Starting at 2 initially builds a tank of 1, then reaches 0 at station 0 and -1 at station 1. No change of starting point can supply the missing unit.

Example 3

Input
gas = [0], cost = [0]
Output
0

The single outgoing leg returns to the same station and costs no fuel. A tank of zero is sufficient; failure requires a negative balance, not merely an empty tank.

Example 4

Input
gas = [0,2,0], cost = [1,0,1]
Output
1

Only station 1 works: its tank progresses 2, 1, 0 through station order 1, 2, 0. Station 2 has no fuel for its first leg. Equal total fuel and cost are feasible even with zero-fuel stations and an empty tank on arrival.

Intuition

Think of each station as a net fuel change: the fuel collected minus the cost of its outgoing leg. The tank must stay nonnegative after every leg, so having enough fuel overall is necessary but does not make every start valid. In Example 1 the changes are -2, -2, -2, +3, +3: station 3 accumulates fuel before crossing the three deficits. If a candidate first fails at station i, all starts between that candidate and i also fail there or earlier, because they lose the nonnegative fuel accumulated before reaching them. This lets one reset eliminate a whole block of candidates.

Approaches

Try Every Start

Solution Details

Reveal Try Every Start: intuition, complexity, and code

Hints

Hint 1
Compare the total gas available with the total cost of the loop before testing any start.
Hint 2
Ask what a failed trip from one station to another says about every station in between.
Hint 3
Track one running tank and restart the candidate just past each failure point.

Edge Cases

  • A single station returns 0 when its fuel covers its self-loop cost, including both values zero; otherwise it returns -1.
  • A negative total net change makes every start impossible, even if one station offers a large temporary surplus.
  • A tank reaching zero after a leg is valid. Example 4 finishes with exactly zero after wrapping.
  • A valid start may be the last station: several failures can move the candidate there before a large final surplus pays for the prefix.
  • For n equal to 100000, total fuel or cost can reach one billion; do not choose an accumulator narrower than Java int.

Common Mistakes and Interview Tips

  • Resetting when the tank is zero rejects affordable legs and can move the candidate beyond the array on a valid all-zero single-station route.
  • Restarting at the failure station reuses a disproved start; move to the station immediately after it.
  • Returning the surviving candidate without checking the total can report a suffix that succeeds while the wrapped circuit fails.
  • Choosing the largest fuel station or largest individual surplus ignores the order of deficits and does not prove circuit feasibility.
  • Forgetting modulo in direct simulation skips the wrapped prefix or accesses outside the array.
  • Treating the final greedy tank as a full-loop tank: it contains only the candidate suffix; the total guard and minimum-prefix invariant justify wraparound.

Key Takeaway

When a first failure eliminates every candidate in a segment, discard that segment as a unit. For a circular resource balance, a global minimum prefix gives a safe starting boundary, and the total balance decides whether wraparound is affordable.