Maths WorkshopMaths problems with full solutions
Exercise 1257Combinatorics › Counting principlesOlympiadAdded 4 Nov 2026

Gas stations on a circular road

#cyclic sequences#prefix sums#extremal choice#cycle lemma

Statement

There are nn gas stations S1,…,SnS_1,\dots,S_n along a circular road, in this cyclic order. Station SiS_i sells gig_i units of fuel and the stretch of road from SiS_i to Si+1S_{i+1} (with Sn+1=S1S_{n+1}=S_1) needs did_i units of fuel. The total fuel available is at least the total fuel needed: ∑gi≥∑di\sum g_i\ge\sum d_i. A car with an empty tank (and unlimited tank capacity) starts at one of the stations, takes all the fuel there, and drives on. Show that one can choose the starting station so that the car completes the full circle without ever running out of fuel.

Hint

Put ai=gi−dia_i=g_i-d_i and look at the partial sums sk=a1+⋯+aks_k=a_1+\dots+a_k. Where should the car start relative to the smallest partial sum?

Show solutionHide solution

Solution

Setup. Let s0=0s_0=0 and sk=a1+⋯+aks_k=a_1+\dots+a_k, so that sn=∑gi−∑di≥0s_n=\sum g_i-\sum d_i\ge0. Let m∈{0,1,…,n−1}m\in\{0,1,\dots,n-1\} be an index minimising sms_m among s0,…,sn−1s_0,\dots,s_{n-1}. Start at station Sm+1S_{m+1}.

The car never runs out. The fuel left on arrival at the station after having driven k−mk-m stretches is sk−sms_k-s_m for m<k≤nm<k\le n, and sn−sm+sjs_n-s_m+s_j after wrapping around.

  • For m<k≤n−1m<k\le n-1 and k=nk=n: sk−sm≥0s_k-s_m\ge0 because sms_m is the minimum of s0,…,sn−1s_0,\dots,s_{n-1} (for k=nk=n we use sn≥0=s0≥sms_n\ge0=s_0\ge s_m).
  • After wrapping: sn+sj−sm≥sj−sm≥0s_n+s_j-s_m\ge s_j-s_m\ge0.

All fuel-on-arrival amounts are non-negative, so the car never runs dry.

Remark. If ∑gi=∑di\sum g_i=\sum d_i the start is unique up to the stations where the minimum is attained. This is the cycle lemma.

Lesson. On a cycle, start right after the minimum of the prefix sums.