Let $n$ be an odd integer different from 1. How many permutations $p$ of $\{1,2, \ldots, n\}$ are there such that

$$
\sum_{i=1}^{n}|p(i)-i|=\frac{n^{2}-1}{2}
$$
The key observation that allows us to hope to solve the exercise is that, by trying cases by hand, we observe that $\frac{n^{2}-1}{2}$ seems to be the maximum value of the considered sum. The terms $|p(i)-i|$ involved in the sum are all differences of two integers, more precisely sums of two integers from $\{1,2, \ldots, n\}$, one counted negatively and the other positively. The total sum thus contains $n$ plus signs and $n$ minus signs. Moreover, each integer from $\{1,2, \ldots, n\}$ appears exactly twice in this sum: once among the $(i)$ and once among the $(p(i))$. We deduce that the sum $\sum_{i=1}^{n}|p(i)-i|$ is necessarily less than the following value, noting $n=2 k+1$:

$$
n+n+\ldots+(k+2)+(k+2)+(k+1)-(k+1)-k-k-\ldots-1-1=\frac{n^{2}-1}{2}
$$

The permutations $p$ we are looking to count correspond to the case of equality, which gives us a lot of information about $p$. For example, let $m$ be an integer strictly greater than $k+1$. This integer is counted twice with a positive sign in the sum, and therefore: $p(m)k+1$. The condition on $p$ is thus equivalent to the two conditions

$$
\{p(1), p(2), \ldots p(k)\}=\{k+2, k+3, \ldots n\}
$$

and

$$
\{p(k+2), p(k+3), \ldots p(n)\}=\{1,2, \ldots k+1\}-\{p(k+1)\}
$$

There are therefore $k(k!)^{2}$ such permutations.

Case $2: p(k+1) \geq k+1$. Then $k+1$ is counted negatively in $|p(k+1)-(k+1)|$. Since it must be counted once positively, this implies that the number $i$ such that $p(i)=k+1$ satisfies $i \leq k+1$. The condition on $p$ is thus equivalent to the two conditions

$$
\{p(1), p(2), \ldots p(k)\}=\{k+1, k+2, \ldots n\}-\{p(k+1)\}
$$

and

$$
\{p(k+2), p(k+3), \ldots p(n)\}=\{1,2, \ldots k\}
$$

There are therefore $(k+1)(k!)^{2}$ such permutations. The final result is thus $(2 k+1)(k!)^{2}=n\left[\left(\frac{n-1}{2}\right)!\right]^{2}$.