×

Simple

# Pre-requisites:

Dynamic Programming

# Problem:

Given an array D[1...N], find max(abs((D[i] + ... + D[j]) - (D[k] + ... D[l])) : 1<= i <= j < k <= l <= N).

# Explanation:

Let us look at the final solution, and work backwards. Let the final solution be due to some i0, j0, k0, l0. We now have two cases:

Case 1: D[k0] + ... + D[l0] <= D[i0] + ... + D[j0].
In this case, we get that among all possible choices of l, D[k0] + ... + D[l] is MINIMUM for l = l0. Else, we could choose such l, and this would give us a larger absolute difference. We also get, that among all 1 <= i <= j <= k0-1, D[i] + ... + D[j] is MAXIMUM.

Case 2: D[k0] + ... + D[l0] > D[i0 + ... + D[j0].
In this case, among all possible choices of l, we choose l0 to give the MAXIMUM value of the sum, and we choose i0, j0 to give the MINIMUM possible sum.

Hence, it would be useful precomputing values that answer "what is the [minimum|maximum] value I can get if I [start|end] at position i?"

### Solution 1

The above setting motivates the following few definitions:
HardLeftMin(i) = Minimum value of the sum of a contiguous subarray whose rightmost point = i.
HardLeftMax(i) = Maximum value of the sum of a contiguous subarray whose rightmost point = i.
SoftLeftMin(i) = Minimum value of the sum of a contiguous subarray whose rightmost point <= i.
SoftLeftMax(i) = Maximum value of the sum of a contiguous subarray whose rightmost point <= i.
HardRightMin(i) = Minimum value of the sum of a contiguous subarray whose leftmost point = i.
HardRightMax(i) = Maximum value of the sum of a contiguous subarray whose rightmost point = i.

Recurrences for the above are easy to find:
HardLeftMin(i) = min(D[i], D[i] + HardLeftMin(i-1)) : either you select position i-1 as well in your subarray and take the best from there, or you don't even take position i-1.
HardLeftMax(i) = max(D[i], D[i] + HardLeftMax(i-1)) : similarly.
HardRightMin(i) = min(D[i], D[i] + HardRightMin(i+1)) : similarly.
HardRightMax(i) = max(D[i], D[i] + HardRightMax(i+1)) : similarly.
SoftLeftMin(i) = min(HardLeftMin(i), SoftLeftMin(i-1)) : either your minimum ends at points i, or it ends at some point <= i-1.
SoftLeftMax(i) = max(HardLeftMax(i), SoftLeftMax(i-1)) : similarly.

Note that, using the above recurrences, we can calculate all the arrays in O(N) time using dynamic programming.

Finally, our case (1) will be covered by SoftLeftMax(j0) - HardRightMin(j0+1), and case (2) will be covered by HardRightMax(j0+1) - SoftLeftMin(j0).
Iterate over all values of j0, and take the maximum, as your answer. This again takes O(N) time to run.

### Solution 2

This solution cleverly disposes of SoftLeftMin, SoftLeftMax() functions and works relying on the following claim.

Claim: Without loss of generality, k0 - j0 = 1. That is, we can consider our optimal phases as being consecutive.

Let us say that OPT returned i, j, k, l, with k-j > 1. Now, consider sum S = D[j+1] + D[j+2] + ... + D[k-1]. If S >= 0, then it can be added to the larger of the two segments [i...j], [k...l]. If S <= 0, then the segment can be added to the smaller of the two segments [i...j], [k...l]. In both cases, it gives us a Delish value atleast as good as the Optimal. Hence, wlog, the two segments we choose are consecutive.

Thus, finally, we iterate over j, and consider abs(LeftMax(j) - RightMin(j+1)) and abs(RightMax(j+1) - LeftMin(j)) as candidates. This approach was used by the Setter.

# Setter's Solution:

Can be found here

# Tester's Solution:

Can be found here

This question is marked "community wiki".

973568379
accept rate: 14%

18.3k347492525

 12 Can anyone explain me this solution of karlheinz_jung???? I have tried my best to understand it, but unfortunately I can't. Those who solved this question help me in figuring a case where my solution went wrong. answered 18 Jun '13, 02:03 2.7k●11●19●27 accept rate: 13% really.. that annoying.. coding right.. it's better to understand someother code(esp. above tutorial) than that purely written code.. (18 Jun '13, 02:13) 1 haha.... I knew it bro.... I was jst thinking, in what world karlheinz would be, when he wrote the code.... I mean how a person can write such a code.... I think he himself cant xplain that.... (18 Jun '13, 02:41) 1 dafaq is this code !!!whre's a main() ?? (18 Jun '13, 11:29) @spandanpathak, It has a main.. but nothing else is understandable. Probably we first need to replace all those variable to simple ones and the format it to understand where the functions start and end.. :P (18 Jun '13, 12:00) ajit_a1★ 1 That code gives me urges that he must have done something which he is hiding behind that dirty code :) (18 Jun '13, 12:09) @shashank_jain: I always prefer akash4983's code for this purpose . least use of templates , least use of all the big constructs and more of a simple oriented approach devoid of problems associated with using abs , fabs , mods etc. (18 Jun '13, 13:03) 1 The code seems to be the output of a decompiler or obfuscator. Some reasons which come to my mind are either he would have done it to optimise the code (eg, compile with gcc -O3 and then decompile) or maybe he cheated from someone else so to make the code look different obfuscated it. (18 Jun '13, 15:37) n2n_5★ 1 I guess he used some translation tool (between programming languages) or other. After doing below conversion(and formatting a little), the code will be fine. Though the basic idea is the same as mine, his implementation is simpler. I fun to work out this puzzle :-) (i_d6->readingBuff),(i_d8->readInt), (i_d12,i_d13,i_d14,i_d15: non-use), (i_d9->sign),(i_d10->value),(i_d11->ch), (i_d7->curChPtr),(i_d18->testCase), (i_d19->i),(i_d20->N),(i_d22->D),(i_d21->maxV), (i_d23->sumMaxL),(i_d24->sumMaxR),(i_d25->sumMinL),(i_d26->sumMinR), (i_d27->pLL),(i_d28->endD),("\45\154\154\144\n"->"%lld\n") (22 Jun '13, 18:24) showing 5 of 8 show all
 9 So, finally, after reading the editorials several times and after searching for some AC solutions, I finally got AC with my solution in C++: /* Problem DELISH @Codechef JUN13 Long Contest * * Main idea is to use DP approach to solve problem in linear time. * We need to maintain 4 vectors that will store: * - Max value of delish starting from left * - Max value of delish starting from right * - Min value of delish starting from left * - Min value of delish starting from right * * Now, since the value of a partial sum is always either >= 0 or <=0 * it is safe to assume that the optimal indexes i,j,k and l * which form the intervals to substract are in fact contigous. * This is because either we add a value to one of them and increase it * , or we add it to the other one which also increases it, towards the * first interval. This yields, wlog, the optimal answer. */ #include #include #include #include #include #include #include #include using namespace std; typedef long long int LL; #define put(x) printf("%lld\n",x) vector max_sum_left(int N, vector arr) { vector res(N); for(int i = 0; i < N; i++) { res[i] = 0; } res[0] = arr[0]; LL currMax = arr[0]; for(int i = 1; i < N; i++) { currMax = max(arr[i],arr[i]+currMax); res[i] = max(res[i-1], currMax); } return res; } vector min_sum_left(int N, vector arr) { vector res(N); for(int i = 0; i < N; i++) { res[i] = 0; } res[0] = arr[0]; LL currMin = arr[0]; for(int i = 1; i < N; i++) { currMin = min(arr[i],arr[i]+currMin); res[i] = min(res[i-1], currMin); } return res; } vector max_sum_right(int N, vector arr) { vector res(N); for(int i = 0; i < N; i++) { res[i] = 0; } res[N-1] = arr[N-1]; LL currMax = arr[N-1]; for(int i = N-2; i >= 0; i--) { currMax = max(arr[i],arr[i]+currMax); res[i] = max(res[i+1], currMax); } return res; } vector min_sum_right(int N, vector arr) { vector res(N); for(int i = 0; i < N; i++) { res[i] = 0; } res[N-1] = arr[N-1]; LL currMin = arr[N-1]; for(int i = N-2; i >= 0; i--) { currMin = min(arr[i],arr[i]+currMin); res[i] = min(res[i+1], currMin); } return res; } LL compute(int N, vector arr) { LL maxDiffAbs = arr[0]-arr[1]; vector leftMax = max_sum_left(N,arr); vector leftMin = min_sum_left(N,arr); vector rightMax = max_sum_right(N,arr); vector rightMin = min_sum_right(N,arr); LL diff; for(int i = 0; i < N-1; i++) { diff = leftMax[i]-rightMin[i+1]; if(diff >= maxDiffAbs) maxDiffAbs = diff; diff = rightMax[i+1]-leftMin[i]; if(diff >= maxDiffAbs) maxDiffAbs = diff; } return maxDiffAbs; } int main() { int t,dim; scanf("%d",&t); for(int i = 0; i < t; i++) { scanf("%d",&dim); vector arr; for(int j = 0; j < dim; j++) { LL elem; scanf("%lld",&elem); arr.push_back(elem); } LL ans = compute(dim,arr); put(ans); } return 0; }  Thanks for this enlightening editorial @pragrame, which allowed me to write what I believe to be a clean solution for this problem! Keep the good work up :D I hope I can also devote some of my time to understand the problem W-string a bit better and hopefully code a solution for it as soon as my time allows me to do so! I hope I can really improve something with this good contest :D Also, @xpertcoder, Thanks for your words! As you probably noticed, those problems are easy ones and those are the concepts which I can say that I feel most comfortable with, so, as you can see I still have a loong way to go :) Best regards, Bruno answered 18 Jun '13, 15:47 2★kuruma 17.5k●72●143●208 accept rate: 8% 4 Nice work, I've also seen some of your tutorials and one time you even explained a problem to me. You have really nice logic, I think if you don't give up you'll become a really strong competitor and be able to fight for the first spots. Also, I've seen you complain a lot about math basis but you shouldn't let it stop you from solving problems. If you do that you'll just be wasting your talent, we should turn our weaknesses into our greatest strengths. In no way I'm saying this to only encourage you, I say this because it's true. (18 Jun '13, 15:57) junior944★ Thanks @junior94, I hope that if I stick around this community my knowledge can grow much, much more and I can grow as a coder and as a person as well :D Everyone's words are a great motivating factor and hopefully things will go better on next month or at least as good as they went on this one :) (18 Jun '13, 16:03) kuruma2★ Your solution is very neat but with an observation you could dove done it a lot cleaner. Think about reversing the array after finding the frist min and max. (18 Jun '13, 18:40) ervin903★ Maybe then it's possible to use only 2 arrays instead of four? (18 Jun '13, 19:18) kuruma2★ I was aiming to the fact that you could've used the first two methods for the other calculations aswell, (19 Jun '13, 00:57) ervin903★
 2 Can anyone pls tell where i am going wrong in this solution.. sol_1 I used the same approach.. and got ACed when i did this : sol_2 Thank you in advance !! :) answered 18 Jun '13, 15:08 1.1k●6●12●15 accept rate: 0%
 0 @pragrame - shouldn't we also include abs(LeftMax(j)-RightMax(j+1)) and abs(LeftMin(j)-RightMin(j+1)) as well because we are considering abs value ? If LeftMin(j)=-2 and RightMax(j+1)=4 and RightMin(j+1)=-10, then the max. abs value among these is abs(LeftMin(j)-RightMin(j+1)) which is not checked! I apologise if I am missing some point ! Thanks ! answered 18 Jun '13, 00:39 11●1 accept rate: 0% 2 Not really. In your example, you say LeftMin(j) - RightMin(j+1) is larger. But note that LeftMin(j) <= LeftMax(j). So your case will be considered (and potentially bettered) in the check for LeftMax(j) - RightMin(j+1). You can check that the same will happen with comparing other pairs - wherever you compare difference of "max with max" or "min with min". (18 Jun '13, 01:30)
 0 I made 2 functions using Kadane's algorithm for max and min. Function max gave me i,j,sum1. Function min gave me k,l,sum2. I took four cases. 1)max(0,arr.length-2) and min(j+1,arr.length-1) result1=Math.abs(sum1-sum2). 2)min(0,arr.length-2) and max(l+1,arr.length-1) result2=Math.abs(sum1-sum2). 3)max(1,arr.length-1) and min(0,i-1) result3=Math.abs(sum1-sum2). 4)min(1,arr.length-1) and max(0,k-1) result4=Math.abs(sum1-sum2). I took maximum of all four results.I satisfied all given cases as well as cases in comments still I got WA always any help or case where it fails please :( :(..... answered 18 Jun '13, 16:48 2★sid4art 1●1 accept rate: 0%
 0 @sid4art i did the same thing and got WA! http://www.codechef.com/viewsolution/2276586 Can someone please point out what is wrong with this approach? answered 19 Jun '13, 14:26 1 accept rate: 0%
 0 http://www.codechef.com/viewsolution/2299047 Can anyone tell whats wrong in this code???? answered 27 Jun '13, 17:04 1 accept rate: 0%
 0 By same reasoning , one can implement Kadane algorithm too. answered 24 Jun '15, 23:24 355●8●19 accept rate: 20%
 0 Can anyone explain me the output for 5 10 3 1 2 9 The output according to setter's or tester's code is 12. How? answered 14 Oct '17, 16:43 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×13,419
×1,495
×935
×27
×15

question asked: 18 Jun '13, 00:19

question was seen: 13,178 times

last updated: 14 Oct '17, 16:43