Given four real numbers $a_1 \lt a_2 \lt a_3 \lt a_4$, rearrange them in such an order $a_{i_1} , a_{i_2} , a_{i_3} , a_{i_4} $ that the sum
$$S = (a_{i_1}-a_{i_2})^2 + (a_{i_2}-a_{i_3})^2 + (a_{i_3}-a_{i_4})^2 + (a_{i_4}-a_{i_1})^2$$ has the least possible value? Can you generalise for $n$ real numbers?
I opened up the equation but it was not of much use.
We can observe that the $3$ inequalities hold:
1) $a_4-a_1 \gt a_3-a_1 \gt a_2-a_1 $
2) $a_4-a_1 \gt a_4-a_2 \gt a_4-a_3 $ .
3) $a_4-a_2 \gt a_3-a_2$
And I believe the order giving the smallest $S$ will be given by $ a_4, a_2, a_1, a_3 $ which is equivalent to the sum if the order is $ a_4, a_3, a_1, a_2 $,
so
$$S = (a_{i_4}-a_{i_2})^2 + (a_{i_2}-a_{i_1})^2 + (a_{i_1}-a_{i_3})^2 + (a_{i_3}-a_{i_4})^2$$
and
$$S = (a_{i_4}-a_{i_3})^2 + (a_{i_3}-a_{i_1})^2 + (a_{i_1}-a_{i_2})^2 + (a_{i_2}-a_{i_4})^2$$
Although I am not sure on how to prove this, any help would be appreciated, thank you.