You are given n non-negative integers $a\_1, a\_2 ,, a\_n$. In a single operation, you take any two integers out of these integers and replace them with a new integer having value equal to difference between those integers; i.e., if the removed integers are $b$ and $c$, you put $|b - c|$ into the set again. You keep applying the operation until you are left with only a single number. So after $n - 1$ steps the game will terminate; you need to find out what is maximum value of the single number left in the end.


I am interested in seeing any polynomial time algorithm for the problem. Currently I don't have any idea about how to solve this problem in less than $\mathcal{O}(n !)$.


If you feel that there does not any polynomial time algorithm, could you please establish in which complexity class it lies? Is it NP complete?


**Summarizing the main arguments:** 


1. You can never get more than maximum of $a\_i$'s.
2. It seems like the number of different terminal values are at most $2^{n - 2}$.
3. Not every expression is valid. An expression corresponds to a combination of + and - signs assigned to integers in the array.
4. Keeping the difference $\geq$ 0 is the main source of difficulty.
5. If we assume that all the expressions are valid, then this problem can be converted to a NP complete problem: the SUBSET-SUM problem. But as not all the expressions are valid, this can give us better ideas/algorithms.
6. Bill Bradley has proved the problem of finding the minimum is NP-hard and he has also shown the construction for point 2; the problem of finding the number of different terminal positions was raised by Johan Wästlund.
7. Brendan McKay's comment extends the argument for finding the maximum is also NP hard. Hence currently we have managed to prove that the problem is **NP hard**.



An Interesting Optimization Problem
[Edit: I had originally posted a proof that finding the *minimum* value was NP-hard; in the comments below, Brendan McKay pointed out how to convert that into a proof that finding the *maximum* value is NP-hard, which was the question asked by the original poster. I have edited this to include a complete answer to the original question.]


Let's refer to the operation of replacing two values $b,c$ in a multiset by the single value $|b-c|$ as a "contraction"; the original poster asks if it is possible to find an ordering of repeated contractions that maximizes the final value.


In this post, we begin by asking if we can find the order with the *minimum* possible value. Specifically, we try to determine whether the minimum value is zero or greater than zero. We will show that deciding this question is NP-complete. (We need to make it into a decision problem ("is it equal to zero?") rather than a minimization problem ("what's the smallest value?") to phrase it in terms of NP-completeness.) We then show how to convert this result into a statement about the maximum rather than minimum value.


Also, to Johan Wastlund's comment above, a slight modification of the construction gives an $O(2^{n-2})$ lower on the maximum number of distinct values that can be produced (which I'll outline at the bottom).




---


Determining if the minimum value is zero is clearly in NP-- we can simply (non-deterministically) guess the correct pattern of steps to produce zero. Showing that it is NP-complete takes a little more effort. We will show this by a reduction from the [partition problem](http://en.wikipedia.org/wiki/Partition_problem). 


Take an instance of the partition problem with inputs $x\_1,...,x\_n$. We are trying to find a subset $S\subseteq \{1,...,n\}$ such that $\sum\_{i\in S}x\_i=\sum\_{i\not\in S}x\_i$.


Set $M\_1=M\_2=2\sum\_i x\_i$. Consider the multiset $\{M\_1,M\_2,x\_1,...,x\_n\}$. Let $f(x,y)=|x-y|$. Since we have taken $M\_1$ to be so large, it follows that


$$f(\cdots(f(f(M\_1,x\_{i\_1}),x\_{i\_2}),\cdots),x\_{i\_k})=M\_1-\sum\_{j=1}^k x\_{i\_j}$$


and similarly for $M\_2$. 


Claim: if there exists a subset $S$ that partitions the integers $x\_1,...,x\_n$, then the minimum value attained by repeated contractions is zero.


Proof: Assume that such an $S$ exists. Then we can apply $f$ to $M\_1$ and the elements of $S$ to get


$$M\_1-\sum\_{i\in S}x\_i$$


and similarly with $M\_2$ and the complement of $S$ to get


$$M\_1-\sum\_{i\not\in S}x\_i$$


As our last step, we can subtract these and get zero. Since the output value is non-negative, then the minimum value is zero.


Claim: if an ordering of repeated contractions produces a final value of zero, then we can produce a partition from this ordering in polynomial time.


Proof: The computation of the final value attained by the repeated contraction corresponds to the evaluation of $f$ on a binary tree with the leaf nodes labeled by $x\_1,...,x\_n$. As pointed out in the comments above, we can "squash" this tree (in $O(n)$ time) and show that the output is equal to the sum $\sum\_i \pm x\_i$. If we let $S$ correspond to the $x\_i$ with a positive index, we have constructed the desired partition.




---


We have shown above that determining whether the smallest value is zero is NP-complete. We now show that this implies the same complexity for the maximum.


Specifically, take $x\_1,...,x\_n$ and observe that the maximum possible value that can be achieved by repeated contraction is $\max\_i x\_i$. We now ask the question: is the maximum final value achieved by repeated contraction equal to $\max\_i x\_i$, or is it strictly smaller?


We will show that deciding this question is NP-complete. It is clearly in NP, since we can exhibit an ordering of contractions that achieves the maximum, if such an ordering exists.


To show that it is NP-complete, we will reduce from the problem of determining if there is an ordering of repeated contractions that equals zero.


Take the multiset $A=\{x\_1,...,x\_n\}$ and consider a new element $M=1+\sum\_i x\_i$. Let $B=A\cup \{M\}$. Note that $M$ is the unique maximum over $B$.


We first show that if the maximum final value for the repeated contraction of $B$ is $M$, then the minimum repeated contraction of $A$ is 0. If there exists a repeated contraction with final sum $M$, then any contraction that involves $M$ must be of the form $f(M,0)$ (otherwise the final sum would be strictly less than $M$) and vice versa. Suppose that we replace $M$ by 0; the same pattern of contractions will now produce a final value of 0. Therefore, we have produced a pattern of contractions on $A\cup \{0\}$ that gives a final value of zero.


In the other direction, we show that if the minimum final value for the repeated contraction of $A$ is 0, then the maximum value for $B$ is $M$. Suppose that the minimum repeated contraction of $A$ is 0. Then consider the identical pattern of contractions on $B$, which will leave us with $M$ and 0; the last contraction will produce $|M-0|=M$


Therefore, we can determine if the minimum final value of $A$ (which was arbitrary) is zero if an only if we can determine if the maximum final value of $B$ is $M$.


Therefore, the problem of determining if the maximum final value of a repeated contraction is equal to the maximum of the original set is NP-complete.




---


Johan Wastlund asked, as a function of $n$, what the maximum number of distinct values can be produced by applying $f$ in all possible combinations? He suggested it might be $2^{n-2}$. The construction above can be modified to show that there are at least this many possibilities.


Let $x\_i=2^i$, for $i=1,...,n-2$. Let $M\_1=2^{n-1}$ and let $M\_2=2^n$. Since


$$M\_z>\sum\_{i=1}^{n-2}$$


(for $z=1,2$) then we still have
$$f(\cdots(f(f(M\_z,x\_{i\_1}),x\_{i\_2}),\cdots),x\_{i\_k})=M\_z-\sum\_{j=1}^k x\_{i\_j}$$


Let $S$ be the subset of indices subtracted from $M\_1$. Then we can construct the sum
$$f(M\_1-\sum\_{i\in S}^k x\_{i},M\_2-\sum\_{i\not\in S}^k x\_{i})= M\_2-M\_1+(\sum\_{i\in S}^k x\_{i})-(\sum\_{i\not\in S}^k x\_{i})$$


This sum assumes a distinct value for each subset $S$ of $\{1,...,n-2\}$, so there are at least $2^{n-2}$ distinct values.


