What is the maximum value of $\ \displaystyle\sum_{i=1}^{n} \vert \sigma(i) - i \vert, $ where $\ \sigma\ $ is a permutation of $\ [n]:= \{1,2,\ldots, n\}\ ?$
Based on a program I ran in Python, the answer seems to be:
$ f(n)= \begin{cases} \frac{n^2 - 1}{2}&\text{if}\, n \text{ odd}\\ \frac{n^2 }{2}&\text{if}\, n \text{ even}\\ \end{cases} $
But my I start getting a memory error for $n=11,$ (which is not surprising as $11!$ is large), so I'm not certain the pattern continues according to that function. Here is my code for $n=8$, which is probably very inefficient:
from itertools import permutations
list8=[a for a in permutations( range(8) )]
'''print(list8)'''
max_list8=0
for x in list8:
sum_diff = 0
for i in range(8):
sum_diff += abs( x[i] - i )
if sum_diff > max_list8:
max_list8 = sum_diff
print(max_list8)
So, I'm curious if that function above is the correct solution to the problem, and if so, why?
You can consider the more general problem: given numbers $a_1 \le a_2 \le \ldots \le a_n$, and $b_1 \le b_2 \ldots \le b_n$ find the maximum ( and the minimum) of $$\sum_{i=1}^n |a_{\sigma(i)} - b_i|$$ over all $\sigma\in \operatorname{Sym}_n$.
You can reduce it to the case $n=2$. Indeed, for $2$ numbers, the largest value is when the order is reversed, and the smallest when the order is preserved.
To check that: $a_1 \le a_2$ and $b_1 \le b_2$ there are $3$ cases: the intervals $[a_1, a_2]$ and $[b_1, b_2]$ are disjoint ( then the order does not matter), the intervals intersect, and one interval contains the other, these last two cases it's easy to check.
Therefore, for the permutation that reverses the order the sum is maximum, for the indentity permutation it is the minimum ( there may be other permutations that achieve the extremum). Note that if the permutations are comparable in the Bruhat order $\sigma \prec \tau$ then $S(\sigma) \le S(\tau)$.
It is interesting also that the same result holds for quadratic sums ( where it is a consequence of Chebyshev inequalities). It would be interesting to know the case of $$\sum_{i=1}^n |a_{\sigma(i)}- b_{i}|^p $$
$\bf{Added:}$ The case of the $l_p$ norm ($p\ge 1$) is entirely similar. The only analysis required ( for $n=2$ ) is the case of disjoint intervals $[a_1, a_2]$ and $[b_1, b_2]$. But here we see that for positive numbers $x$, $y$, $z$, $t$, with $x+y= z+t$, we have $x^p + y^p \le z^p + t^p$ if $z,t$ are more "extreme" than $x,y$ ( $\min(z,t) \le \min(x,y)$, $\max(z,t) \ge \max(x,y)$).
$\bf{Added:}$ Some details for the "reduction to $n=2$ case". Consider a permutation $\sigma \in S_n$. Assume that there exists $i< j$ such that $\sigma(i) < \sigma(j)$. Now consider the permutation $\tau \colon = \sigma \circ (i,j)$. Note that $\tau(k) = \sigma(k)$, for $k \not \in \{i, j\}$, $\tau(i) = \sigma(j)$, $\tau(j) = \sigma(i)$. From the $n=2$ case we get
$$|a_{\tau(i)} - b_i| + |a_{\tau(j)} - b_{j}| \ge |a_{\sigma(i)} - b_i| + |a_{\sigma(j)} - b_{j}|$$ so $$S(\tau) = \sum_k |a_{\tau(k)} - b_k|\ge S(\sigma) = \sum_k |a_{\sigma(k)}- b_k|$$
Moreover, we have for the number of inversions $n(\cdot)$
$$n(\tau) \ge n(\sigma) + 1$$
Therefore, in a finite number of steps, from any permutation $\sigma$ we reach the reversal permutation $\tau(i) = n+1-i$, with no decrease in the value of $S(\cdot)$. So the maximum is achieved at the reversal.
$\bf{Added:}$ It was at the insistence of @Ingix: that I had to provide a clearer argument. Here is the problem: we have a function from a finite set $$\operatorname{Sym}_n \ni \sigma \mapsto S(\sigma)= \sum_i |a_{\sigma(i)} - b_i|$$
and we want to show that $\sigma= w_0$ ( the longest element), $w_0(i) = n+1-i$ achieves the maximum.
The lemma for $n=2$ only shows this: if $\sigma\ne w_0$, (that is, form some $i< j$ we have $\sigma(i) < \sigma(j)$), then there exists a $\tau$ obtained from $\sigma$ such that $S(\tau) \ge S(\sigma)$ ( note that the inequality might not be strict). Does this prove that the maximum is achieved at $w_0$? Apriori no! We might just go on with $\sigma\mapsto \tau \mapsto \eta\mapsto \cdots $, and and some point the values of $S(\cdot)$ stabilize, and we never reach in this way $w_0$. So necessarily we'll have some cycling. But cycling doe not happen here because there is another variable $n(\sigma)$ that increases. It may not be right away that the number of inversions strictly increases. However, we have another variable that is easily seen to strictly increase: $S_2(\sigma) = \sum_i |a_{\sigma(i)} - b_i|^2$. So now we can modify the problem to this: maximize
$$\sigma \mapsto S_1(\sigma) + \epsilon\cdot S_2(\sigma)$$
Now $\sigma =w_0$ is the unique point of maximum ( assume the values $a_i$ distinct and $b_i$ distinct). From here we take $\epsilon\to 0$ and conclude that $w_0$ is a point of maximum.