1

I've got this problem on my last exam, which I struggle to deal with.

Let's say we have array of $N$ integers (it can be float too, but let's say integers for sake of simplicity. We need to sum those numbers, but we can only use operation of summing two adjacent numbers. Goal of algorithm is to sum this sequence, so maximum of absolute values fromm sum from this operation will be lowest possible.

For example: Let's say we have array -2 5 -3. First we sum 5 and -3, so the temporary sum is 2, and our sequence changes to -2 2. Then we sum -2 and 2, so now our temporary sum is 0. As we see, maximum of temporary sums was 2, and we can't get any lower. (summing -2 and 5 would give us 3, which is higher that 2).

My goal on that exam was to find best complexity algorithm, and prove its correctness.

What I tried:
First thought was to use greedy algorithm, so just sum those two numbers which give lowest temporary sum right now.
Problem is, I can neither prove it is valid way to solve it, nor find any counterexample.

yossi
  • 11
  • 2
  • https://cs.stackexchange.com/q/59964/755 – D.W. Aug 15 '20 at 07:29
  • It isn't clear whether you are trying to minimize the sum of two adjacent numbers in a single iteration (trivial) or throughout all the algorithm runtime. – Acsor Aug 15 '20 at 11:42
  • throughout all the algorithm runtime, sorry, forgot to mention – yossi Aug 15 '20 at 11:51
  • Just to confirm. The example in the block quote is part of the statement and not your own interpretation of the problem, right? So, it is part of the problem that they don't count the maximum absolute value of the elements in the input, right? – plop Aug 15 '20 at 16:23
  • Greedy algorithm does not work. – John L. Aug 15 '20 at 16:31
  • Consider any strategy for adding and look at the last state that exhibits a number attaining the maximum absolute value present in that strategy. Consider the state immediately before. The addition that is going to be performed either adds two numbers of the same sign, or two numbers of different signs. Show that in the first case, either all numbers are of the same sign, or you can modify the next step to produce a smaller maximum. In the second case, either the step is going to add the largest number to the smallest or you can change the strategy to produce a smaller maximum. – plop Aug 15 '20 at 17:15
  • Observe also that in the first case, where the last step that exhibits the maximum absolute value added two numbers of the same sign, must also be the last step of the whole strategy. So, this step added two numbers. These two numbers are sums of a partition of the input. If we feed the elements of the partition as input to the problem and consider resulting optimal strategies, observe that we can replace our strategy so far by those optimal strategies, without affecting the overall maximum. By induction on the size of the input, we show that among all optimal strategies there is always one... – plop Aug 15 '20 at 17:21
  • ... that whenever there are numbers of different signs, it adds the maximum number to the minimum number. If at any step all numbers have the same sign, then it doesn't matter in which order one adds. The maximum absolute value from that point on will be the sum of all elements. – plop Aug 15 '20 at 17:21

0 Answers0