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
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
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 :(…
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.
Maybe then it’s possible to use only 2 arrays instead of four?
I was aiming to the fact that you could’ve used the first two methods for the other calculations aswell,
@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?
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")
By same reasoning , one can implement Kadane algorithm too.
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?
@dsahapersonal assuming t=1,n=5,d={10,3,1,2,9}, that is the correct answer ! We need to basically find two contiguous subarrays (no intersecting point) such that the absolute difference(if the diff is negative,it turns positive) between the sum of each subarray is maximum .Now,the main thing to note here is that the length of the subarray could also be 1. Therefore , if you consider these two subarrays : {10,3} and {1} then the difference is absolute(1-(10+3))=absolute(-12) = 12 ! Also if you take any other pair of subarrays ,you will find that the absolute difference is not greater than that.
The sixth function is wrongly written. It should be leftmost not rightmost.
HardRightMax(i) = Maximum value of the sum of a contiguous subarray whose leftmost point = i.
The above solution provided in tutorial is giving tle while submitting in the java and the same solution give ac while submitting in java why?
Java Solution->https://www.codechef.com/viewsolution/21872923
C++ Solution->https://www.codechef.com/viewsolution/21872897