First time here? Checkout the FAQ!
+2 votes
Given an array of $n$ elements find the maximum continuous sum in it. For example consider the below array of $n=6$.

23 4 -10 2 15 1

Answer is 35.
asked in Algorithm Challenges by Veteran (285k points)  
edited by | 308 views
yes, this is an issue of programmers- even I always used to correct the code and not think of the algorithm. Since you are a good explainer you can try explaining your code and when you do, you should get the correct solution also :) The only thing here is to find the start and end of the subarray. But I'll give a solution without finding this.

ok....sir...i will be upload the correct solution...smiley

See my answer and then code. Because there is no advantage in solving this question correctly- key is to solve all such questions correctly :)
ok i got  the idea from ur answer sir...i will try it .
See again, I had written wrong earlier :O

2 Answers

+1 vote

I asked this question to explain dynamic programming. I hope question is clear. Okay now, our challenge is to find the start and end of a subarray in the given array so that sum is maximum. i.e., given array


we have to find $i$ and $j$ such that $\sum_{k=i}^j A[k]$ is maximum. (Well, we don't need to output $i$ and $j$ but just the corresponding sum).

Okay, so one important thing is we are looking at "subarray" and not "subsequence" and hence the elements in the required subarray must be continuous. This makes the problem harder (subsequence solution will just be the sum of all positive array numbers).

Now, lets see if the problem has any sub-part which can be solved and which overlaps (if so we can do dynamic programming).

So, let me take an array element - say $A[i]$. There are only 2 choices for it- either it is in the required subsequence or it is not. So what is the condition for inclusion?

  1. Let us assume the array ends at i for now.
  2. We include A[i] in our required subarray, if the sum of the subarray ending at i-1 + A[i] produces a sum greater than any other subarray.

Well, the second statement here is the key- it gives our problem sub problems and moreover they are overlapping. So, lets try to apply it. What we need is an array say "sum" to store the max continuous sum for each position of the array ($sum[i]$ gives the maximum sum of the substring ending at $i$)  and one more element say MAXS, which stores the max sum seen so far. Now, we can say

sum[i] = max(sum[i-1] + A[i], A[i]);
if (sum[i] > MAXS), MAXS = sum[i];

with sum[1] = A[1] and MAXS = A[1] as the boundary conditions. 

Now, trying to code this? - 10 lines? :P

answered by Veteran (285k points)  
edited by
ok sir.. i hav checked again...u made earlier sum[i] = max(MAXS,sum[i-1] + A[i]]);

now is.... sum[i] = max(sum[i-1] + A[i], A[i]);...rt?
yes. And unfortunately that is the MOST important step and what I wanted to say :(
arjun sir, scroll back to check by answer i have updated it...u plz check the dynamic prgamming is correctly implemented in program or not.
yes, it looks correct :)

yes..i hav done in less than 10 linessmiley

0 votes
answered by (139 points)  

Top Users May 2017
  1. akash.dinkar12

    3578 Points

  2. pawan kumarln

    2314 Points

  3. Bikram

    1950 Points

  4. Arjun

    1852 Points

  5. sh!va

    1682 Points

  6. Debashish Deka

    1296 Points

  7. Devshree Dubey

    1282 Points

  8. Arunav Khare

    1122 Points

  9. Angkit

    1072 Points

  10. LeenSharma

    1028 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 29 - Jun 04
  1. Arunav Khare

    246 Points

  2. Arjun

    202 Points

  3. pawan kumarln

    108 Points

  4. Rupendra Choudhary

    94 Points

  5. Niharika 1

    90 Points

22,909 questions
29,243 answers
27,744 users