Ctrl K

Where to Start the Lap

Medium
There are n fuel stations around a circular track, numbered from 0 in the driving direction. At station i you can take fuel[i] units of fuel, and the road from it to the next station (from the last one back to station 0) burns burn[i] units. The tank has no limit and starts empty. Return the index of the station you can start from and drive a full lap without the fuel ever going negative. If there is no such station, return -1. If a suitable station exists, it is guaranteed to be unique. Trying every start takes O(n²); aim for a single pass.
Example 1
fuel=[2, 6, 1, 5], burn=[3, 2, 6, 2] → 3

Start at station 3: the tank holds 5 − 2 = 3, then 3 + 2 − 3 = 2, then 2 + 6 − 2 = 6, then 6 + 1 − 6 = 1, and the lap is done. From any other station the tank runs dry somewhere.

Example 2
fuel=[3, 1, 2], burn=[2, 3, 2] → -1

There are 6 units of fuel in total but the lap needs 7, so no start works.

Example 3
fuel=[5], burn=[5] → 0

A single station: you take 5 and spend exactly that to drive back to it.

Constraints
  • 1 ≤ n ≤ 10⁵
  • len(fuel) = len(burn) = n
  • 0 ≤ fuel[i], burn[i] ≤ 10⁴
  • If a suitable station exists, it is unique
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumFewest Jumps to the End MediumReach the Last Cell MediumAs Many Trades as You Like MediumBiggest Number From Pieces Medium
loading Python…
Input
fuel=[2, 6, 1, 5], burn=[3, 2, 6, 2]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 47 tests on the server, hidden ones included.