954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
Team Colleague
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
Team Colleague
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
Team Colleague
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
 

i guess u can try some recursive algorithm for this .but with that 0(n) is still tough .. with recursive u can try maximum combinations while keeping efficiency ..

joshilay
Light Poster
31 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

Can somene publicate a code for this solution
Thanks

Atanas
Newbie Poster
1 post since Apr 2007
Reputation Points: 9
Solved Threads: 0
 
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
Team Colleague
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
 

Hello
I'm thinking in a way, but I don't know if it can be implemented in O(n) way

first, we take the integral of the array
for example A = [1,2,3,4,5]
would give us the integral B = [1,2,6,10,15]

then we shall find the maximum difference between two items in this integral, such that the maximum index > minimum index

and these indexes ( indices ) are the first and end of the maximum subset

does that make sense ?

omko
Newbie Poster
4 posts since Mar 2008
Reputation Points: 35
Solved Threads: 0
 

Sorry, but I still don't understand what is "subset with maximum sum". Can anyone explain or give me more examples?

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

No problem, I also didn't get the idea when I heard about it

let's take for example this array ( [1,-2,3,-4])
what's the consecutive subsets ( subarrays ) which we can derive from it, and their sums

[1] = 1
[1,-2] = -1
[1,-2,3] = 2
[1,-2,3,-4] = 2-
[-2] = -2
[-2,3] = 1
..
[3] = 3
[3,-4] = -1

[-4]

so, the algorithm must return the subarray [3] which has the biggest sum which is 3

more examples ?

omko
Newbie Poster
4 posts since Mar 2008
Reputation Points: 35
Solved Threads: 0
 
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.

I dont understand, sum of the subset [5,10] is clearly greater than that of [5,-2,10]

hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 

I know , but in this question we want consecutive items, 5 and 10 are not, it's not a { } set
it's [ ], so we care about ordering

in my example , I can't get [1,-4] for example

got it ?

omko
Newbie Poster
4 posts since Mar 2008
Reputation Points: 35
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You