Skip to content
ISEGORIABenjamin Haire

Daily Mathematics / Solutions

Solution for 2 November 2026

The card that day was an original math olympiad problem. Try it first; the solution is folded away below.

Math Olympiad problem

Reversing blocks led by their maximum

Card: Reversing blocks led by their maximum

The numbers \(1,2,\dots,2026\) are written in a row in some order. A move: choose at least two consecutive numbers, the leftmost of which is the largest of them, and reverse their order. Prove that only finitely many moves are possible, and find the largest possible number of moves.

Show the solution

We work with \(n\) numbers; the answer is \(2^{n-1}-1\), so \(2^{2025}-1\) for \(n=2026\). Write the row as \(a_1,\dots,a_n\). A move reverses a block \(a_i,\dots,a_j\) with \(j>i\) and \(a_i=\max(a_i,\dots,a_j)\).

Why the obvious measures are not enough. A move leaves \(a_1,\dots,a_{i-1}\) alone and puts \(a_j<a_i\) in position \(i\), so the row strictly decreases in dictionary order. This proves termination, but only with the bound \(n!-1\). The number of inversions is no better: it can go up, for example \(5,1,2,3,4\to 4,3,2,1,5\) raises it from 4 to 6. We need a finer potential.

Step 1: the potential. For an entry \(v\) let \(r(v)\) be the number of entries smaller than \(v\) standing to its right. Put \(w(0)=0\) and \(w(k)=2^{k-1}\) for \(k\ge 1\); note that \(w\) is strictly increasing and \(\sum_{q=0}^{m-1}w(s+q)=w(s+m)-w(s)\) for \(s\ge1\), while \(\sum_{q=0}^{m-1}w(q)=w(m)-1\) for \(m\ge1\). Define \[\Phi=\sum_{v} w\bigl(r(v)\bigr).\] Since \(r(v)\le v-1\), we have \(\Phi\le\sum_{v=2}^{n}2^{v-2}=2^{n-1}-1\).

Step 2: every move lowers \(\Phi\) by at least 1. Fix a move on a block of length \(L\ge 2\) whose first entry is \(M\). Entries outside the block keep their value of \(r\): the set of values to the right of an entry before the block is unchanged, and nothing to the right of the block moves. For an entry \(v\) of the block let \(s(v)\) be the number of smaller entries to the right of the block, and let \(x(v)\), \(y(v)\) be the numbers of smaller block entries to the right and to the left of \(v\). Before the move \(r(v)=s(v)+x(v)\); after it \(r(v)=s(v)+y(v)\), because the reversal swaps left and right inside the block.

For \(M\) itself \(x(M)=L-1\) and \(y(M)=0\), so with \(s=s(M)\) its term falls from \(w(s+L-1)\) to \(w(s)\). List the other block entries by value as \(v_1<v_2<\dots<v_{L-1}\). The block entries smaller than \(v_t\) are exactly \(v_1,\dots,v_{t-1}\), so \(y(v_t)\le t-1\); and every entry smaller than \(v_t\) is smaller than \(M\), so \(s(v_t)\le s\). Hence the new term of \(v_t\) is at most \(w(s+t-1)\). Writing \(B=\sum_t w(r_{\text{old}}(v_t))\ge 0\) for the old terms of these entries, \[\Delta\Phi\le\sum_{t=1}^{L-1}w(s+t-1)-\bigl(w(s+L-1)-w(s)\bigr)-B.\]

If \(s=0\), the sum equals \(w(L-1)-1\), so \(\Delta\Phi\le -1-B\le -1\). If \(s\ge 1\), the sum equals \(w(s+L-1)-w(s)\), so \(\Delta\Phi\le -B\). When \(B\ge1\) we are done. When \(B=0\), every \(v_t\) has \(s(v_t)=0<s\), so its new term is at most \(w(t-1)<w(s+t-1)\) and the inequality above is strict; thus \(\Delta\Phi<0\). As \(\Phi\) is an integer, \(\Delta\Phi\le -1\) in every case.

Consequently any sequence of moves is finite and has at most \(\Phi(\text{start})\le 2^{n-1}-1\) moves.

Step 3: a sequence of \(2^{n-1}-1\) moves. Whether a move is legal depends only on the relative order of the entries involved, so we may sort any block of positions as if it were a row on its own, as long as no move touches entries outside it. Let \(f(m)\) be the length of the following procedure, which turns a decreasing row of length \(m\) into an increasing one. Start from \(n,n-1,\dots,1\). First, keeping \(n\) in front, sort positions \(2,\dots,n\) (a decreasing row of length \(n-1\)) in \(f(n-1)\) moves, reaching \(n,1,2,\dots,n-1\). Next reverse the whole row, which is legal because \(n\) comes first and is the largest: this gives \(n-1,\dots,2,1,n\). Finally sort positions \(1,\dots,n-1\) in \(f(n-1)\) moves. So \(f(n)=2f(n-1)+1\) with \(f(1)=0\), that is \(f(n)=2^{n-1}-1\).

Conclusion. The process always stops, and the largest possible number of moves is \(2^{2025}-1\). A computer search for \(n\le 8\) shows even more: the longest play from any starting row is exactly \(\Phi\) of that row.

Check. An exhaustive search over all rows with \(n\le 8\) (up to 40320 rows) confirmed that every legal move lowers \(\Phi\) by at least 1, that the longest play from every row equals \(\Phi\), and that the maximum is \(1,3,7,15,31,63,127\) for \(n=2,\dots,8\); the recursive construction was also simulated for \(n=5,10,15\) and gave exactly \(2^{n-1}-1\) legal moves ending in the sorted row.

Answer. \(2^{2025}-1\) moves (in general \(2^{n-1}-1\) for \(n\) numbers)

All solutions · 日本語