|
|
Subject:
Divide and Conquer Java question
Category: Computers > Programming Asked by: tokes-ga List Price: $30.00 |
Posted:
01 Dec 2004 17:47 PST
Expires: 06 Dec 2004 10:01 PST Question ID: 436874 |
I need tp devise an O(n) divide and conquer algorithm to compute the maximum subsequence sum of a list. The maximum subsequence sum of a list containing a0, a1, a2...an-1 is the subsequence ai, ai+1, ...aj such that the sum of these elements is maximum amongst all possible subsequences. Notice that if all the ak are negative, then the empty subsequence has the maximum sum of 0. Say, for example, if A=[-10,2,4,-10,3,4,5,-5,7,-1], then MaxSubsequenceSum.maxSubsequenceSum(A, 10) returns 14, corresponding to the subsequence (3,4,5,-5,7). |
|
There is no answer at this time. |
|
Subject:
Re: Divide and Conquer Java question
From: crythias-ga on 02 Dec 2004 09:15 PST |
In pseudo basic, the logic is: 1) maxValue=0 maxSize=0 maxIndex=0 2) loop through all array elements. If all values are <=0, then stop and return 0 3) loop through all 0 to n-1 array elements again at each index k, array sumof[k,0]= value of array A[k] if value of array sumof[k,0] > maxValue then { maxValue= value of array A[k] maxSize= 0 maxIndex= k } loop through elements (k+1) to (n-1) at each index m, array sumof[k, m-k]=value of array sumof[k, m-k-1] + value of array A[m] if value of array sumof[k, m-k] > MaxValue then { maxValue = value of array sumof[k, m-k] maxSize= m-k maxIndex= k } end loop end loop Return maxValue (although maxSize gives you number-1 of elements in the array and maxIndex gives you starting element) This is a free comment. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |