Subfield: Combinatorics


Question: Let $n$ be a positive integer. Define a chameleon to be any sequence of $3 n$ letters, with exactly $n$ occurrences of each of the letters $a, b$, and $c$. Define a swap to be the transposition of two adjacent letters in a chameleon. Prove that for any chameleon $X$, there exists a chameleon $Y$ such that $X$ cannot be changed to $Y$ using fewer than $3 n^{2} / 2$ swaps.

Solution:
To start, notice that the swap of two identical letters does not change a chameleon, so we may assume there are no such swaps.

For any two chameleons $X$ and $Y$, define their distance $d(X, Y)$ to be the minimal number of swaps needed to transform $X$ into $Y$ (or vice versa). Clearly, $d(X, Y)+d(Y, Z) \geqslant d(X, Z)$ for any three chameleons $X, Y$, and $Z$.

Lemma. Consider two chameleons

$$
P=\underbrace{a a \ldots a}_{n} \underbrace{b b \ldots b}_{n} \underbrace{c c \ldots c}_{n} \text { and } Q=\underbrace{c c \ldots c}_{n} \underbrace{b b \ldots b}_{n} \underbrace{a a \ldots a}_{n} .
$$

Then $d(P, Q) \geqslant 3 n^{2}$.

Proof. For any chameleon $X$ and any pair of distinct letters $u, v \in\{a, b, c\}$, we define $f_{u, v}(X)$ to be the number of pairs of positions in $X$ such that the left one is occupied by $u$, and the right one is occupied by $v$. Define $f(X)=f_{a, b}(X)+f_{a, c}(X)+f_{b, c}(X)$. Notice that $f_{a, b}(P)=f_{a, c}(P)=f_{b, c}(P)=n^{2}$ and $f_{a, b}(Q)=f_{a, c}(Q)=f_{b, c}(Q)=0$, so $f(P)=3 n^{2}$ and $f(Q)=0$.

Now consider some swap changing a chameleon $X$ to $X^{\prime}$; say, the letters $a$ and $b$ are swapped. Then $f_{a, b}(X)$ and $f_{a, b}\left(X^{\prime}\right)$ differ by exactly 1 , while $f_{a, c}(X)=f_{a, c}\left(X^{\prime}\right)$ and $f_{b, c}(X)=f_{b, c}\left(X^{\prime}\right)$. This yields $\left|f(X)-f\left(X^{\prime}\right)\right|=1$, i.e., on any swap the value of $f$ changes by 1 . Hence $d(X, Y) \geqslant$ $|f(X)-f(Y)|$ for any two chameleons $X$ and $Y$. In particular, $d(P, Q) \geqslant|f(P)-f(Q)|=3 n^{2}$, as desired.

Back to the problem, take any chameleon $X$ and notice that $d(X, P)+d(X, Q) \geqslant d(P, Q) \geqslant$ $3 n^{2}$ by the lemma. Consequently, $\max \{d(X, P), d(X, Q)\} \geqslant \frac{3 n^{2}}{2}$, which establishes the problem statement.
In any chameleon $X$, we enumerate the positions in it from left to right by $1,2, \ldots, 3 n$. Define $s_{c}(X)$ as the sum of positions occupied by $c$. The value of $s_{c}$ changes by at most 1 on each swap, but this fact alone does not suffice to solve the problem; so we need an improvement.

For every chameleon $X$, denote by $X_{\bar{c}}$ the sequence obtained from $X$ by removing all $n$ letters $c$. Enumerate the positions in $X_{\bar{c}}$ from left to right by $1,2, \ldots, 2 n$, and define $s_{\bar{c}, b}(X)$ as the sum of positions in $X_{\bar{c}}$ occupied by $b$. (In other words, here we consider the positions of the $b$ 's relatively to the $a$ 's only.) Finally, denote

$$
d^{\prime}(X, Y):=\left|s_{c}(X)-s_{c}(Y)\right|+\left|s_{\bar{c}, b}(X)-s_{\bar{c}, b}(Y)\right| .
$$



Now consider any swap changing a chameleon $X$ to $X^{\prime}$. If no letter $c$ is involved into this swap, then $s_{c}(X)=s_{c}\left(X^{\prime}\right)$; on the other hand, exactly one letter $b$ changes its position in $X_{\bar{c}}$, so $\left|s_{\bar{c}, b}(X)-s_{\bar{c}, b}\left(X^{\prime}\right)\right|=1$. If a letter $c$ is involved into a swap, then $X_{\bar{c}}=X_{\bar{c}}^{\prime}$, so $s_{\bar{c}, b}(X)=s_{\bar{c}, b}\left(X^{\prime}\right)$ and $\left|s_{c}(X)-s_{c}\left(X^{\prime}\right)\right|=1$. Thus, in all cases we have $d^{\prime}\left(X, X^{\prime}\right)=1$.

As in the previous solution, this means that $d(X, Y) \geqslant d^{\prime}(X, Y)$ for any two chameleons $X$ and $Y$. Now, for any chameleon $X$ we will indicate a chameleon $Y$ with $d^{\prime}(X, Y) \geqslant 3 n^{2} / 2$, thus finishing the solution.

The function $s_{c}$ attains all integer values from $1+\cdots+n=\frac{n(n+1)}{2}$ to $(2 n+1)+\cdots+3 n=$ $2 n^{2}+\frac{n(n+1)}{2}$. If $s_{c}(X) \leqslant n^{2}+\frac{n(n+1)}{2}$, then we put the letter $c$ into the last $n$ positions in $Y$; otherwise we put the letter $c$ into the first $n$ positions in $Y$. In either case we already have $\left|s_{c}(X)-s_{c}(Y)\right| \geqslant n^{2}$.

Similarly, $s_{\bar{c}, b}$ ranges from $\frac{n(n+1)}{2}$ to $n^{2}+\frac{n(n+1)}{2}$. So, if $s_{\bar{c}, b}(X) \leqslant \frac{n^{2}}{2}+\frac{n(n+1)}{2}$, then we put the letter $b$ into the last $n$ positions in $Y$ which are still free; otherwise, we put the letter $b$ into the first $n$ such positions. The remaining positions are occupied by $a$. In any case, we have $\left|s_{\bar{c}, b}(X)-s_{\bar{c}, b}(Y)\right| \geqslant \frac{n^{2}}{2}$, thus $d^{\prime}(X, Y) \geqslant n^{2}+\frac{n^{2}}{2}=\frac{3 n^{2}}{2}$, as desired.