To find the maximum subset sum in an array.
Hi all,
Can anybody help me with a an algorithm to find the maximum subset sum in an array.
It would be better if the solution is of O(n).
[example: {5,-2,10,-4} the maximum is 5 + (-2) + 10 => 13].
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
The maximum would be 5 + 10, just toss out all negative values.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I dont want the maximum sum . I want the subset with maximum sum.
{5,-2,10,-4}
Here the subset with maximum sum is {5,-2,10,-4}. The program should output {5,-2,10}
I think u got my point.
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
First generate all the subsets, then add the totals.
The one with the biggest total wins?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Then you mean the maximum sublist sum, not subset.
First, you can convert the list into a list of cumulative sums, turning [5,-2,10,-4] into [0,5,3,13,9]. Then walk through the list of cumulative sums, saving the smallest value so far and the maximum difference between what you see as the current value and what is currently the smallest value.
Of course, you don't have to actually create a list of cumulative sums; you can keep track of the cumulative sum as you walk through the first list.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Rashakil,
Seems like a good logic let me try to implement this.
But dont u think that then also I have to walk thru the array twice?
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
First generate all the subsets, then add the totals.
The one with the biggest total wins?
iamthwee,
I will go for this logic as a last option. I think the algo will be bulky as it will consume lots of space.
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
iamthwee,
I will go for this logic as a last option. I think the algo will be bulky as it will consume lots of space.
Well what are you doing? What is this algorithm for?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Well what are you doing? What is this algorithm for?
I have seen this ques in some website, I just wanted to know the efficient way to do it. I have some solution, but doesnt seems efficient.
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
Rashakil,
Seems like a good logic let me try to implement this.
But dont u think that then also I have to walk thru the array twice?
Well, no, you don't; I've implemented this before. And even if you did, what's worng with walking through the array twice? It's still linear time.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Well, no, you don't; I've implemented this before. And even if you did, what's worng with walking through the array twice? It's still linear time.
Agreed its linear time. Let me try with one pass.
Anyway thanks for the help.
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
Can somene publicate a code for this solution
Thanks
Nope -- its a secret :) Seriously, you should attempt to solve it yourself and then start your own thread to ask questions.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
This problem is covered in detaill in Jon Bentley's Programming Pearls (Second Edition)
ISBN 81-7808-022-2 (Pearson Education)
see column 8 : Algorithm Design Techniques (pages 77-86)
Even if you have been programming for a number of years,
but have not seen this book so far, do yourself a favour. Get a copy.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287