<p><em>This is not a complete answer, but it may be helpful.</em></p>

<p>A piece of <em>Mathematica</em> code finds a <em>counterexample</em> of your greedy strategy: Consider the list of integers $\{51,54,55,70,98\}$. </p>

<p>Your algorithm gives $\{98,51,70,54,55\}$ (Have I misunderstood your idea?) and the sum is 83. </p>

<p>The program produces four permutations with the sum 125: $\{54, 98, 51, 70, 55\}, \{54, 70, 51, 98, 55\}, \{55, 98, 51, 70, 54\}, \{55, 70, 51, 98, 54\}$.</p>

<blockquote>
  <p>It seems that you should put the maximal(s) between minimal(s) as far as possible. </p>
</blockquote>

<p>You can post other greedy strategies and prove (or <em>disprove!</em>) them by employing (maybe combining them) classic proof strategies such as mathematical induction, contradiction, and exchange argument.</p>

<hr>

<p><strong>Edit:</strong></p>

<p>Actually, I don't quite understand why my answer (instead of that of @Marzio De Biasi) was accepted. Maybe the OP only want to know whether his/her greedy strategy is correct or not. Whatever, please refer to the answer of @Marzio De Biasi (and also other ones) for a complete solution.</p>

CC BY-SA 3.0
2014-12-13T18:19:17.957Z
To me this answer makes the OP's question more interesting, because it makes it clear that some more sophisticated idea will likely be needed to solve the problem.
David Eppstein
CC BY-SA 3.0
2014-12-13T13:51:27.313Z
2014-12-14T01:49:31.863Z
2014-12-14T01:49:31.863Z
hengxin
<p>I found that the problem has been used in a student programming contest.</p>

<p>... if you still want to try to solve it by yourself don't move the mouse over the area below !!! :-)</p>

<blockquote class="spoiler">
  <p>The programmingn contest is the Bubble Cup 7DC, Belgrade 2012.<br><br>The key idea to solve it is to consider the middle element of the given array after sorting it in increasing order (if the number of elements is even then pick the value between the two middle elements); it splits the set in two halves $A$ and $B$.<br><br>Then it is easy to prove that an optimal solution cannot contain three consecutive elements in increasing (or decreasing) order $... x_i &lt; x_j &lt; x_k ...$ because it is enough to move the middle element at the end of the array and the sum cannot decrease after the trasformation. So an optimal solution must proceed in a zig-zag style. Then we can prove that the optimal solution cannot contain two consecutive elements from the same side. Suppose that we have somewhere $... x_i, x_j, x_k ...$ with $x_i&gt; x_j &lt; x_k$ and $x_i, x_j, x_k \in B$. Then then the remaining sequence must contain three consecutive elements from $A$  $...x'_i,x'_j,x'_k...$ ordered as $x'_i &lt; x'_j &gt; x'_k$. Then $A \ni x'_j &lt; x_j \in B$ can be swapped preserving the inequalities and strictly increasing the value of the sum.<br><br>For the detailed proof see: <a href="http://www.bubblecup.org/Media/Default/BlogPost/Booklet%202012.pdf">Bubble Cup 7DC - Problem C: MaxDiff</a><br><br>(it contains other nice problems)</p>
</blockquote>

CC BY-SA 3.0
2014-12-13T21:08:37.010Z
A "joke" solution occurs to me: once you notice the obvious zig-zag property, this becomes a linear optimizition over the permutohedron of $x$ and can be solved with linear programming. Not the kind of thing that gets you far in a programming contest though :) (or in the real world for that matter).
Sasho Nikolov
CC BY-SA 3.0
2014-12-13T21:14:47.453Z
@SashoNikolov: it could be another answer :-) However in the spirit of the contest I "protected" the solution with a spoiler.
Marzio De Biasi
CC BY-SA 3.0
2014-12-13T23:36:29.160Z
Can you explain what is meant by "it is enough to move the middle element at the end of the array"?  I had a hard time parsing that sentence.  Do you mean moving $x_j$ somewhere?  where?  swapping $x_j$ with $x_k$?
D.W.
CC BY-SA 3.0
2014-12-14T00:56:44.637Z
@D.W.: suppose that in the optimal solution there are 3 consecutives increasing elements: $x_a, .... , x_i < x_j < x_k, ...., x_b$ then the contribution to the total sum is $(x_k - x_j) + (x_j - x_i)$ if we remove $x_j$ the total sum doesn't change, so we can move $x_j$ at the end (or at the beginning) of the sequence and get an increment of $max( |x_a - x_j|, |x_b - x_j|) \geq 0$.
Marzio De Biasi
CC BY-SA 3.0
2014-12-13T20:50:37.173Z
2014-12-13T21:11:56.683Z
2014-12-13T21:11:56.683Z
Marzio De Biasi
<p>Although I'm late to the game, it's still useful to have a reference.</p>

<p>This is a variation of the dart board design problem. The $x_i$s are the scores for each wedge of the dart board. The goal is to make sure if one decide to hit a particular slice, there will be a huge penalty if the player misses: A large gap in the scores between two adjacent wedge. Hence it is formulated as maximize 
$$\sum_{i=1}^n |x_{\pi(i)}-x_{\pi(i-1)}|^p$$</p>

<p>where $p\geq 1$ and $\pi$ is a permutation and $\pi(0)=\pi(n)$.
Your version that maximize
$$\sum_{i=2}^n |x_{\pi(i)}-x_{\pi(i-1)}|^p$$
is the hoopla board design problem. </p>

<p>Curtis have a greedy algorithm for both problems, and it is very close to your algorithm$^1$. The only difference is you are growing the list in only one direction, but Curtis grow the list in both direction: add an element that maximizes the difference to either end of the list.</p>

<p><strong>Update :</strong></p>

<p>Actually, this follows directly from an much older result that keep getting rediscovered: (maximum) TSP problem where the distance matrix is a <a href="http://en.wikipedia.org/wiki/Supnick_matrix" rel="nofollow">Supnick matrix</a>. </p>

<ol>
<li>S.A. Curtis, Darts and hoopla board design, <em>Information Processing Letters</em>, Volume 92, Issue 1, 16 October 2004, Pages 53-56, ISSN 0020-0190, <a href="http://dx.doi.org/10.1016/j.ipl.2004.06.005" rel="nofollow">http://dx.doi.org/10.1016/j.ipl.2004.06.005</a>.</li>
</ol>

CC BY-SA 3.0
2014-12-14T01:53:31.443Z
Now this problem is not only interesting, but also practical and meaningful, which make it even more interesting.
hengxin
CC BY-SA 3.0
2014-12-14T01:21:49.977Z
2014-12-14T08:31:41.713Z
2014-12-14T08:31:41.713Z
Chao Xu
<p>The starting point with evaluating any greedy algorithm is to try running it on a bunch of examples, to see if it always works on all of them.  (Hint: write a program, generate one million test cases, and see if it computes the right answer for every one of them.)</p>

<p>If it fails on <em>any</em> of them, you have a counterexample, and you know your algorithm is incorrect.</p>

<p>If it works on all of them, then you can start looking for a proof of correctness.  Typically this will involve a kind of "swapping" or "exchange argument". See any good textbook in the section on greedy algorithms; it will typically present examples of greedy algorithms and proofs that use this strategy.</p>

CC BY-SA 3.0
2014-12-13T13:56:13.560Z
A piece of code finds a counterexample of the greedy strategy of the OP: ${51,54,55,70,98}$. $\{54,98,51,70,55\}$ gives the sum 125, which is bigger than that given by $\{98,51,70,54,55\}$.
hengxin
CC BY-SA 3.0
2014-12-13T06:56:52.083Z
2014-12-13T06:56:52.083Z
D.W.
<p>Here is the algorithmic problem I'm trying to solve:</p>

<p>Given a list of integers $x_1, x_2, \dots, x_n$ find a permutation $p_1, p_2, \dots, p_n \in [n]$ that maximises the sum $\sum_{i=1}^{n-1}|x_{p_{i+1}} - x_{p_i}|$.</p>

<p>I think this can be solved in $O(n\log n)$, I thought of sorting the list and returning a list where first element is maximum element, next element is minimum, next is maximum of those which are left, etc. But this leads to nowhere, I cannot prove that it's correct, so probably it isn't. So, any tips how to tackle this problem?</p>

CC BY-SA 3.0
2014-12-13T02:00:24.903Z
What is $|\cdot|$ of a list? Do you mean: Given a list of integers $x_1, x_2, \dots, x_n$, find a permutation $p_1, p_2, \dots, p_n \in [n]$ that maximizes $\sum_{i=1}^{n-1} |x_{p_{i+1}} - x_{p_i}|$?
Michael Blondin
CC BY-SA 3.0
2014-12-13T05:52:24.180Z
Your idea sounds like a kind of greedy strategy. Try the [exchange argument](http://people.cs.pitt.edu/~kirk/cs1510/notes/greedynotes.pdf) for its correctness proof.
hengxin
CC BY-SA 3.0
2014-12-13T18:40:19.263Z
I don't understand the close votes here, and the comments and answers that seem to be responding to a different question, "prove that my greedy algorithm is correct". What the OP actually asked for is a different algorithm, assuming (accurately given hengxin's answer) that greedy is incorrect. It seems research-level and on-topic for this board.
David Eppstein
<ds.algorithms>
Finding a permutation $p$ of $x_1, x_2, \dots, x_n$ which maximises $\sum_{i=1}^{n-1}|x_{p_{i+1}}-x_{p_i}|$
qiubit
# Finding a permutation $p$ of $x_1, x_2, \dots, x_n$ which maximises $\sum_{i=1}^{n-1}|x_{p_{i+1}}-x_{p_i}|$

(Asked by: username_0 on 2014-12-12)

Here is the algorithmic problem I'm trying to solve:

Given a list of integers $x\_1, x\_2, \dots, x\_n$ find a permutation $p\_1, p\_2, \dots, p\_n \in [n]$ that maximises the sum $\sum\_{i=1}^{n-1}|x\_{p\_{i+1}} - x\_{p\_i}|$.

I think this can be solved in $O(n\log n)$, I thought of sorting the list and returning a list where first element is maximum element, next element is minimum, next is maximum of those which are left, etc. But this leads to nowhere, I cannot prove that it's correct, so probably it isn't. So, any tips how to tackle this problem?

**Tags:** ds.algorithms
> **Comments:**
> **username_1**: What is $|\cdot|$ of a list? Do you mean: Given a list of integers $x_1, x_2, \dots, x_n$, find a permutation $p_1, p_2, \dots, p_n \in [n]$ that maximizes $\sum_{i=1}^{n-1} |x_{p_{i+1}} - x_{p_i}|$?  
> **username_2**: Your idea sounds like a kind of greedy strategy. Try the [exchange argument](http://people.cs.pitt.edu/~kirk/cs1510/notes/greedynotes.pdf) for its correctness proof.  
> **username_3**: I don't understand the close votes here, and the comments and answers that seem to be responding to a different question, "prove that my greedy algorithm is correct". What the OP actually asked for is a different algorithm, assuming (accurately given hengxin's answer) that greedy is incorrect. It seems research-level and on-topic for this board.  

---

## 4 answers

### Answer by username_2 on 2014-12-13. Score: 7 | Accepted answer

*This is not a complete answer, but it may be helpful.*

A piece of *Mathematica* code finds a *counterexample* of your greedy strategy: Consider the list of integers $\{51,54,55,70,98\}$.

Your algorithm gives $\{98,51,70,54,55\}$ (Have I misunderstood your idea?) and the sum is 83.

The program produces four permutations with the sum 125: $\{54, 98, 51, 70, 55\}, \{54, 70, 51, 98, 55\}, \{55, 98, 51, 70, 54\}, \{55, 70, 51, 98, 54\}$.

> It seems that you should put the maximal(s) between minimal(s) as far as possible.

You can post other greedy strategies and prove (or *disprove!*) them by employing (maybe combining them) classic proof strategies such as mathematical induction, contradiction, and exchange argument.

---

**Edit:**

Actually, I don't quite understand why my answer (instead of that of @Marzio De Biasi) was accepted. Maybe the OP only want to know whether his/her greedy strategy is correct or not. Whatever, please refer to the answer of @Marzio De Biasi (and also other ones) for a complete solution.

> **Comments:**
> **username_3**: To me this answer makes the OP's question more interesting, because it makes it clear that some more sophisticated idea will likely be needed to solve the problem.  

---

### Answer by username_4 on 2014-12-13. Score: 8

I found that the problem has been used in a student programming contest.

... if you still want to try to solve it by yourself don't move the mouse over the area below !!! :-)

> The programmingn contest is the Bubble Cup 7DC, Belgrade 2012.  
>   
> The key idea to solve it is to consider the middle element of the given array after sorting it in increasing order (if the number of elements is even then pick the value between the two middle elements); it splits the set in two halves $A$ and $B$.  
>   
> Then it is easy to prove that an optimal solution cannot contain three consecutive elements in increasing (or decreasing) order $... x\_i < x\_j < x\_k ...$ because it is enough to move the middle element at the end of the array and the sum cannot decrease after the trasformation. So an optimal solution must proceed in a zig-zag style. Then we can prove that the optimal solution cannot contain two consecutive elements from the same side. Suppose that we have somewhere $... x\_i, x\_j, x\_k ...$ with $x\_i> x\_j < x\_k$ and $x\_i, x\_j, x\_k \in B$. Then then the remaining sequence must contain three consecutive elements from $A$ $...x'\_i,x'\_j,x'\_k...$ ordered as $x'\_i < x'\_j > x'\_k$. Then $A \ni x'\_j < x\_j \in B$ can be swapped preserving the inequalities and strictly increasing the value of the sum.  
>   
> For the detailed proof see: [Bubble Cup 7DC - Problem C: MaxDiff](http://www.bubblecup.org/Media/Default/BlogPost/Booklet%202012.pdf)  
>   
> (it contains other nice problems)

> **Comments:**
> **username_5**: A "joke" solution occurs to me: once you notice the obvious zig-zag property, this becomes a linear optimizition over the permutohedron of $x$ and can be solved with linear programming. Not the kind of thing that gets you far in a programming contest though :) (or in the real world for that matter).  
> **username_4**: @SashoNikolov: it could be another answer :-) However in the spirit of the contest I "protected" the solution with a spoiler.  
> **username_6**: Can you explain what is meant by "it is enough to move the middle element at the end of the array"?  I had a hard time parsing that sentence.  Do you mean moving $x_j$ somewhere?  where?  swapping $x_j$ with $x_k$?  
> **username_4**: @D.W.: suppose that in the optimal solution there are 3 consecutives increasing elements: $x_a, .... , x_i < x_j < x_k, ...., x_b$ then the contribution to the total sum is $(x_k - x_j) + (x_j - x_i)$ if we remove $x_j$ the total sum doesn't change, so we can move $x_j$ at the end (or at the beginning) of the sequence and get an increment of $max( |x_a - x_j|, |x_b - x_j|) \geq 0$.  

---

### Answer by username_7 on 2014-12-14. Score: 4

Although I'm late to the game, it's still useful to have a reference.

This is a variation of the dart board design problem. The $x\_i$s are the scores for each wedge of the dart board. The goal is to make sure if one decide to hit a particular slice, there will be a huge penalty if the player misses: A large gap in the scores between two adjacent wedge. Hence it is formulated as maximize
$$\sum\_{i=1}^n |x\_{\pi(i)}-x\_{\pi(i-1)}|^p$$

where $p\geq 1$ and $\pi$ is a permutation and $\pi(0)=\pi(n)$.
Your version that maximize
$$\sum\_{i=2}^n |x\_{\pi(i)}-x\_{\pi(i-1)}|^p$$
is the hoopla board design problem.

Curtis have a greedy algorithm for both problems, and it is very close to your algorithm$^1$. The only difference is you are growing the list in only one direction, but Curtis grow the list in both direction: add an element that maximizes the difference to either end of the list.

**Update :**

Actually, this follows directly from an much older result that keep getting rediscovered: (maximum) TSP problem where the distance matrix is a [Supnick matrix](http://en.wikipedia.org/wiki/Supnick_matrix).

1. S.A. Curtis, Darts and hoopla board design, *Information Processing Letters*, Volume 92, Issue 1, 16 October 2004, Pages 53-56, ISSN 0020-0190, <http://dx.doi.org/10.1016/j.ipl.2004.06.005>.

> **Comments:**
> **username_2**: Now this problem is not only interesting, but also practical and meaningful, which make it even more interesting.  

---

### Answer by username_6 on 2014-12-13. Score: 1

The starting point with evaluating any greedy algorithm is to try running it on a bunch of examples, to see if it always works on all of them. (Hint: write a program, generate one million test cases, and see if it computes the right answer for every one of them.)

If it fails on *any* of them, you have a counterexample, and you know your algorithm is incorrect.

If it works on all of them, then you can start looking for a proof of correctness. Typically this will involve a kind of "swapping" or "exchange argument". See any good textbook in the section on greedy algorithms; it will typically present examples of greedy algorithms and proofs that use this strategy.

> **Comments:**
> **username_2**: A piece of code finds a counterexample of the greedy strategy of the OP: ${51,54,55,70,98}$. $\{54,98,51,70,55\}$ gives the sum 125, which is bigger than that given by $\{98,51,70,54,55\}$.  

---

