PuzzleReels
Sections

The Fuel Around the Track

The maths · Episode 7 · 2:11 · gives the answer

The puzzle

A circular racetrack has cans of fuel scattered round it, of different sizes and in no particular order, and between them they hold exactly enough for one lap. You may start beside any can you like with an empty tank, and you pour in each can as you pass it.

Show that there is always a can you can start from and finish the lap.

The maths, properly

Number the cans 1,…,n in driving order. Can i holds fi, and the leg from can i to the next costs ci, with fuel measured in the distance it buys. Exactly a lap of fuel means

∑ifi=∑ici

Drive the lap from can 1 and let the level go negative if it wants to. The level after leg i is

Li=∑k≤i(fk−ck),L0=0

and because the fuel is exactly a lap, Ln=0 as well.

Proof one: start after the lowest point

This is the film's proof. Pick m with Lm as low as it gets, and start at can m+1 (can 1 if m=n).

Starting there, the level after each leg is the old level measured up from the bottom of the dip. Going on from m to n it is Lk−Lm. Past can n the track wraps round to can 1, and since Ln=0 the level is Lk−Lm again. Each of those is at least zero, because Lm is the lowest value. Nothing ever goes below empty, so the lap closes.

The same picture shows nothing else works. Start after any point that is not a lowest one and you meet the lowest point on the way round, where the level is Lm−Lk<0.

Proof two: shrink the track

Induction on the number of cans. With one can, f1=c1 and any start works.

With more, some can has fi≥ci, since otherwise the cans could not add up to a lap. You can leave that can and arrive at the next with fuel to spare. So pour the next can into it and delete that stop: one can now holds fi−ci+fi+1 and the leg after it costs ci+1. There is still exactly a lap of fuel and one can fewer, so by induction some start works on the smaller track.

That same start works on the original. Starting at the merged can, you start at can i, and you reach can i+1 with exactly the fuel the merged can promised. Starting anywhere else, you reach can i with some level ℓ≥0, and leaving it you have ℓ+fi−ci≥0, so you arrive at can i+1 in the same state as on the smaller track.

Run backwards this is an algorithm. Drive from can 1 with an empty tank. The moment you get stuck, throw away everything so far and start again from the next can. One pass, nothing remembered except the current level.

Proof three: one unit of slack

Take whole numbers xi=fi−ci and suppose they add up to +1 instead of 0. Let Li be the partial sums as before, so Ln=1, and let m be the last index in 0,…,n−1 where Lm is lowest. Start after m.

Going on from m to n, every later Lk is strictly above Lm and the final one is 1−Lm≥1. After the wrap the level is 1+Lk−Lm≥1. So every partial sum is strictly positive.

Only the last lowest point works. Start after an earlier tie and you reach that later one with level 0. Start after anything higher and you reach a lower point with level 0 or less. So exactly one of the n rotations has every partial sum above zero. This is the cycle lemma, usually credited to Dvoretzky and Motzkin (1947) and to Raney (1960). It holds for any whole-number steps, not just the ±1 of the next section.

A worked case: the film's track

The film's track has 5 cans holding 5, 9, 2, 10, 4 and legs costing 8, 3, 6, 4, 9. Both add up to 30, one lap. Driving from can 1 on the imaginary tank gives levels −3, 3, −1, 5, 0, so the lowest is −3, after leg 1, and the start is can 2.

Every start on the film's track
Start atFuel left after each legResult
can 1−3runs dry on leg 1, 3 short
can 26, 2, 8, 3, 0gets round
can 3−4runs dry on leg 1, 4 short
can 46, 1, −2runs dry on leg 3, 2 short
can 5−5runs dry on leg 1, 5 short

Exactly 1 of the five starts works, which is the point of choosing this track. It looks like luck until the proof lands.

0 0−33−150 can 1can 2can 3can 4can 5can 1
Start at can 1
0 062830 can 2can 3can 4can 5can 1can 2
Start at can 2

Start at can 1 and the curve dips below the axis, and where it dips lowest is just before can 2. Start at can 2 and the whole curve lifts above the axis. Nothing about it has changed except where zero is.

More than one start can work. When the level touches its lowest value more than once, every touch gives a start. The film's three other tracks, solved by the same rule:

The three tracks the film solves at the end
CansLegsStarts that work
7, 17, 11, 3, 116, 14, 22, 3, 44 and 5
5, 22, 1, 518, 8, 3, 42
8, 7, 22, 44, 19, 15, 33

Break the track

Change any can or leg, roll a random track, or shuffle the cans: every start is driven out on the spot.

A random track of six cans, one lap of fuel, and what happens from each start
Start atFuel in the canNext leg costsFrom here
can 156dry on leg 1→2
can 260dry on leg 5→6
can 335dry on leg 3→4
can 499dry on leg 5→6
can 549dry on leg 5→6
can 642finishes the lap

Generalising

Too little or too much fuel. If the cans hold less than a lap, no start can work, whatever the order, because the level at the end of the lap is negative from any start. If they hold more, take the surplus out of any one can and the track has exactly a lap, so a start exists, and it still works with the surplus put back. The table below counts every case:

Less fuel than a lap, exactly a lap, more than a lap
CansLess: tracks…with a startExact: tracks…with a startMore: tracks…with a start
1604466
210604444106106
31,75805805801,7581,758
428,72208,0928,09228,72228,722
5466,1360116,304116,304466,136466,136

How many starts can there be? One, as on the film's track, and up to all of them. If every can matches its leg (fi=ci everywhere), every start works. With five cans holding 1, 0, 0, 0, 0 and legs costing 0, 1, 0, 0, 0, exactly the cans 1, 3, 4, 5 work, which is every one but can 2. The number that work is the number of times the level touches its minimum, nothing else. For every track of up to 5 cans with cans and legs from 0 to 3, the starts that work are exactly the cans after a lowest point.

Not only integers. Proofs one and two only compare levels, so they never use whole numbers. Whole numbers are what the script can enumerate, and what proof three needs for "strictly positive" to mean "at least one unit".

Fuel along the whole road

Now take the stations away. A road of length L goes round a circle. Fuel is supplied along it at a density g(x) and burned at a density c(x), both per kilometre, and the net rate is f=g−c. Exactly enough fuel for a lap means

∫0Lf(x)dx=0

Measure the road in laps, so L=1; nothing else changes. Let

Φ(x)=∫0xf

be the level on arriving at x if you had started at 0 on the imaginary tank. Because ∫f=0, Φ(x+1)=Φ(x), so Φ lives on the circle. Start at s with an empty tank and the level at x, for x from s round to s+1, is

Fs(x)=∫sxf=Φ(x)−Φ(s)

The first half is one line: Fs≥0 everywhere says Φ(x)≥Φ(s) everywhere. The second half is where continuity does the work that checking finitely many cans did. Φ is continuous and periodic, so it is a continuous function on a circle, which is closed and bounded, and the extreme value theorem says it attains its minimum. Start at a point s where it does, and Fs(x)=Φ(x)−Φ(s)≥0 the whole way round.

An example you can check

Take f(x)=cos2π(x−a)+bsin4π(x−a) with a=0.07 and b=0.3. It averages to zero, and Φ has a closed form, so there is an exact answer to compare with. f changes sign twice. It goes from + to − at x=0.32, where Φ is highest (0.2660). It goes from − to + at x∗=0.82: bisection gives 0.820000000000, f(x∗)=0 to machine precision, and Φ′′(x∗)=f′(x∗)=2π(1−2b)=2.5133>0. So x∗ is a minimum, and it is the global one, Φ(x∗)=−0.0523. The only start that works is x∗.

lowest, x = 0.82 0¼½¾1
The level Φ, and the same road with 8 stations
0.820.070.320.570.82
Start at x = 0.82 and the whole curve lifts

The right panel is the same curve with the start moved to x∗: the whole of it lifts onto the axis, touching it only at the two ends. That is the film's picture with the stations taken out.

A flatter bottom is possible. With b=0.5 instead, f has a triple zero at x∗, so Φ′′ vanishes there as well and Φ grows like h4: doubling the distance from x∗ multiplies the rise by 16.0. The minimiser is still unique and still x∗, but the bottom is very flat.

Stations are point masses

The film's problem is this one with a particular f. A can is fuel poured in all at once, which is a point mass of size fi at its position, and a leg is a steady burn. Fuel in the film is measured in the distance it buys, so the burn is 1 per unit distance, c(x)=1, and the lap is L=30 with the cans at positions 0, 8, 11, 17, 21:

The film's track as a road of length 30
CanPosition xFuel poured in thereLevel Φ on arriving
1050
289−3
31123
41710−1
52145

Between cans Φ falls with slope −1. At a can it jumps up by the fuel there. So its lowest values are the levels on arriving at a can, just before the jump: here −3 at x=8, which is can 2. "Start just after the lowest point" and "start at the can where the arrival level is lowest" are the same instruction.

One technical point is worth saying once. At a jump, Φ has to be defined by the level on arriving, which counts only the cans strictly before x. That version is lower semi-continuous, meaning it never sits above its own limits from either side, because every jump goes up: cans only add fuel. That is exactly what the extreme value argument needs, so the minimum is still attained. If fuel could also be removed in an instant, a jump down would let Φ get lower and lower without ever reaching its infimum, and there would be no best start. So the density may burn, and the point masses may only supply.

How many starts

The starts that work are the minimisers of Φ, and since Φ is lower semi-continuous that set is closed and not empty. Beyond that, anything goes:

  • One, the usual case, as on the film's track and the example above.
  • Several isolated ones, when the level touches its lowest value more than once, as in the discrete problem.
  • An interval, when there is a stretch with no fuel and no burn at the lowest level. Take f=−1 on (0,14), 0 on (14,34) and +1 on (34,1): the good starts are exactly 0.25 to 0.75, half of the road, with Φ=−0.25 all along it.
  • Anything closed. For any non-empty closed set K on the circle, Φ(x)=dist(x,K) is continuous, periodic and 1-Lipschitz, so it is the integral of a bounded f with ∫f=0, and its minimisers are exactly K. A Cantor set will do. In that case f is merely integrable, not continuous, which is why the proof above never asked for more.

The same lemma, rotated

Rotate the circle to start at a minimiser x∗ and write Φ~(t)=Φ(x∗+t)−Φ(x∗) for 0≤t≤1. Then Φ~≥0 and Φ~(0)=Φ~(1)=0: a continuous bridge turned into an excursion. This is the continuous cycle lemma. Vervaat (1979) applied it to a Brownian bridge, whose minimum is almost surely unique, and showed that the rotated path is a Brownian excursion. That is a pointer only; everything above is the deterministic half.

From stations to a continuum

Cut the example road into n equal cells and make each a legitimate station problem. Cell i contributes its net fuel di=Φ(i+1n)−Φ(in), as a can of max(di,0) with station i at i/n and a leg costing max(−di,0). That is the one honest choice: each cell's net is exact, and what is lost is only what happens inside it. The level on arriving at station j is then exactly Φ(j/n), so the starts that work are the grid points where Φ is lowest.

Φ falls from its maximum to x∗ and rises again, with no other turning point (checked below), so the lowest grid point has to be one of the two beside x∗. That puts it less than 1/n away, and the table shows how much less. Both statements lean on there being a single dip: with two dips of nearly the same depth, a coarse grid can pick the wrong one.

The same road cut into n stations
Stations nStart that worksPosition j/nDistance to the minimiserDistance × nStarts that work
8can 80.8750000.0550000.4401
16can 140.8125000.0075000.1201
64can 530.8125000.0075000.4801
256can 2110.8203130.0003120.0801
1024can 8410.8203130.0003120.3201

At eight stations the winner is the eighth, at 0.875, 0.055 from x∗. With a thousand and twenty-four stations it is 0.000312 away. Exactly one start works at every size.

More fuel than a lap

Add a constant surplus: f+δ with δ>0. A start s works exactly when

Φ(s)+δs≤Φ(x)+δxfor everyx∈[s,s+1]

with Φ continued periodically. For δ<0 the point x=s+1 breaks this at once, so no start works. For δ=0 it is the minimisers. For δ>0 every start that worked at δ=0 still works, because the surplus only raises Fs, so the set of good starts can only grow as δ does. Near a minimum with Φ′′=κ>0 it grows into an interval about 2δ/κ long: start ε after x∗ and the tightest point is just before you come round to x∗ again, where the surplus has put you δ ahead, so you need roughly κε2/2≤δ.

A surplus of δ per lap: the starts that work
Surplus δStarts fromtoLengthLength ÷ √(2δ/κ)
0.00010.81990.82890.00891.00
0.0010.81960.84770.02811.00
0.010.81600.90230.08630.97
0.050.80030.98260.18240.91

Counting votes

Give every can the value +1 or −1: a can holding one unit with no leg, or a leg costing one unit with no can. Take m+1 of the first kind and m of the second. That is 2m+1 steps adding up to +1, and the cycle lemma says that among the 2m+1 rotations of any such sequence exactly one stays positive throughout. So the fraction of sequences that already do is 12m+1, and the count is

12m+1(2m+1m)

which is the Catalan number, the same one that counts balanced bracket strings of m pairs.

One unit of slack, steps of +1 and −1
mStepsSequences with total +1…always positive from the startSequences ÷ steps
13313 / 3 = 1
2510210 / 5 = 2
3735535 / 7 = 5
4912614126 / 9 = 14
51146242462 / 11 = 42
61317161321716 / 13 = 132
71564354296435 / 15 = 429
81724,310143024310 / 17 = 1430

An election is the same thing. Candidate A gets a votes and B gets b with a>b. Counting the votes one at a time, how often is A strictly ahead throughout? The total is a−b, and the same argument gives exactly a−b good rotations out of a+b, so the fraction is

a−ba+b

This is Bertrand's ballot theorem.

Counting votes, one at a time
Final countOrders the votes can come inA ahead throughoutFractionGood rotations of each order
2 to 1311/31
3 to 1421/22
3 to 21021/51
4 to 21551/32
5 to 356141/42
6 to 228141/24
7 to 4330903/113
8 to 3165755/115

Checking it

None of the above is left to the proofs. scripts/fuel.mjs reads the film's own track.json, re-derives every level and every working start in it, and asserts that they agree. Then it checks the rest, and the continuous section as well.

Every small track. Every can holds 0 to 3 and every leg costs 0 to 3, for up to 5 cans. That is 1,118,480 tracks, of which 125,024 hold exactly a lap. Every one of those has a start that works, and the working starts are exactly the cans after a lowest point.

Every track with exactly a lap of fuel: how many starts work
CansTracks1 start2 starts3 starts4 starts5 startsNone
144————0
2442816———0
358034816864——0
48,0924,6922,248896256—0
5116,30466,02031,58013,2004,4801,0240

Random tracks. Three ways of finding a start are run on random tracks: start after the lowest point, drive until stuck and restart, and shrink the track. Each returns a start that finishes. On tracks of up to 1000 cans, the full list of working starts, found by trying every one, is compared with the cans after the lowest points. On 20 tracks of 100,000 cans the linear methods are checked by driving the lap.

Random tracks, checked three ways
CansTracksStarts that work, on averageMost seen
55,0001.053
502,0001.074
10001001.032
100,00020——

The continuous example. Φ is checked against numerical integration of f, its two sign changes are found by bisection, Φ′′>0 is checked at the minimum, and the global minimum is confirmed on a grid of 2,000,000 points and refined to many digits, with exactly 2 turning points. The film's track, read as a road, gives the same arrival levels as the film. Every discretisation above is built as a real station problem and solved by the same code as the rest, and its working starts are compared with the cans after the lowest level. The surplus intervals are found on a grid of 40,000 points with a sliding minimum.

The lab. The track lab above runs the functions in site/lib/fuel-core.mjs, the same text the script imports, so a green start on the page is a start the script asserted. On 30,000 random tracks of 3 to 12 cans with whole units from 0 to 9, its drive agrees with the lap check from every start, the working starts are exactly the cans after the lowest level on all 15,000 tracks with one lap of fuel, there is always at least one of them, and none works with less than a lap.

The cycle lemma. For whole-number steps between −3 and 3, up to seven of them, there are 70,553 sequences with total +1, and every one has exactly one good rotation. For ±1 steps the tables above count every sequence.

Where this shows up

  • The gas station problem. This is the standard interview version, usually asked with a circle of stations and one tank. The expected answer is one pass keeping a running total and restarting after any failure, which is proof two.
  • Ballot counting. The probability that the winner is strictly ahead throughout is a−ba+b, and the proof by rotation is the one above.
  • Counting in combinatorics. The cycle lemma is a standard way of counting lattice paths, bracket strings and trees, and a rotation argument of this kind is often the quickest way to show a count is 1n of another one.

Try it yourself

1. Suppose the cans hold slightly less than a lap. Is there still always a start?

No. From any start, the level at the end of the lap is the total fuel minus the total cost, which is below zero, so you are stuck somewhere on the way round. In the table of tracks above, none of those with less than a lap has a start that works. So "exactly enough" is doing real work.

2. Can all n starts work? Can exactly n−1?

Both, for every n from 2 to 12. If every can equals its leg, every start works. For n−1, put one unit in can 1 and a leg costing one unit after can 2, and nothing anywhere else. The cans 1, 3, 4, 5 work for five cans, and can 2, where you are stuck at once, is the one that fails.

3. Suppose the fuel is in whole units and the cans hold one unit more than a lap. How many starts leave you with at least one unit in the tank after every leg?

Exactly one. That is the cycle lemma, proof three. Among the 2m+1 rotations of any ±1 sequence with total +1, exactly one is positive throughout, and counting those sequences gives the Catalan numbers in the table above.

4. Can you find a start in a single pass round the track, without finding the lowest point first?

Yes. Drive from can 1 keeping only the current level. When it goes negative, drop it back to zero and start again from the next can. At the end, if the total fuel is at least a lap, the last can you restarted from is a start that works. That is proof two as an algorithm. It uses nothing but the current level, so it works the same for 100,000 cans as for five.

5. Fuel is supplied continuously, with net density f(x)=sin2πx on the circle of length 1. Where should you start?

At x=0, which is the same point as x=1. Here Φ(x)=1−cos2πx2π, which is never negative. Its minimum is 0, at x=0, and that is where f goes from negative to positive. It is the only start that works. The opposite point is the worst one: Φ is highest at x=0.5, where f goes from positive to negative, with value 0.3183, and starting there you finish −0.3183 below empty. A minimum is where f changes sign from − to +; the other sign change is a maximum.