TITLE: Maximizing the sum $\sum\limits_{i=1}^nx_ix_{i+1}$ subject to $\sum\limits_{i=1}^nx_i=0$ and $\sum\limits_{i=1}^nx_i^2=1$
QUESTION [11 upvotes]: Is there an efficient way of solving the following problem?

Given $x_i\in \mathbb R$, and that $\sum\limits_{i=1}^nx_i=0$ and $\sum\limits_{i=1}^nx_i^2=1$. I want to maximize $\sum\limits_{i=1}^nx_ix_{i+1}$ where we take $x_{n+1}=x_1$.

I don't know if this is relevant/useful at all but maybe representing the systems as $\vec{x}^TI\,\,\vec{x}$ and $\vec{x}^T\{\delta_{i , \,\,i+1}\}\,\,\vec{x}$ might help?
Thanks.

REPLY [2 votes]: This is similar to alex.jordan's answer, but from a different perspective. Let $P\ $ be the permutation matrix $(\delta_{i+1,j})$ and $A=\frac12(P+P^\top)$. Let also $\mathbf{u}=(1,1,\ldots,1)^\top$. Then
$$
\max\{\mathbf{x}^\top P\mathbf{x} : \|\mathbf{x}\|=1,\ \mathbf{x}\perp \mathbf{u}\}
= \max\{\mathbf{x}^\top A\mathbf{x} : \|\mathbf{x}\|=1,\ \mathbf{x}\perp \mathbf{u}\}.
$$
Since $\mathbf{u}$ is an eigenvector of $A$ corresponding to the eigenvalue $1$, the RHS is equal to the maximum eigenvalue of $A$ when the eigenvalue $1$ is excluded. Note that $A$ is a circulant matrix. In general, for a circulant matrix whose first row is some $(c_0, c_1, \ldots, c_{n-1})$, the eigenvalues are given by
$$
\lambda_k = c_0 + c_1\omega_k + c_2\omega_k^2 + \ldots + c_{n-1}\omega_k^{n-1};
\quad k=0,1,\ldots,n-1,
$$
where $\omega_k=\exp\left(2\pi k\sqrt{-1}/n\right)$. The eigenvector corresponding to $\lambda_k$ is
$$
\mathbf{v}_k = (1,\,\omega_k,\,\omega_k^2,\ldots,\omega_k^{n-1})^\top.
$$
For our $A$, we have $c_1=c_{n-1}=\frac12$ and $c_k=0$ otherwise. Therefore
$$
\lambda_k=\frac12(\omega_k + \omega_k^{n-1})=\cos\left(2\pi k/n\right).
$$
Hence the maximum of the eigenvalues (excluding $1$) is $\lambda_1=\cos\left(2\pi/n\right)$. Taking the real part of $\mathbf{v}_1$, we get a real eigenvector $\mathrm{Re}(\mathbf{v}_1) = \left(1, \mathrm{Re}(\omega_1), \mathrm{Re}(\omega_1^2), \ldots, \mathrm{Re}(\omega_1^{n-1})\right)^\top$. Normalize it, we obtain the solutions $(x_1,\ldots,x_n)=\mathrm{Re}(\mathbf{v}_1)/\|\mathrm{Re}(\mathbf{v}_1)\|$. As robjohn has worked  out, the normalizing factor is $\sqrt{\dfrac2n}$ and hence $x_j = \sqrt{\dfrac2n}\cos\left(\dfrac{2\pi j}{n}\right)$.