To find the maximum subset sum in an array.

Reply

Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

To find the maximum subset sum in an array.

 
0
  #1
Jul 3rd, 2006
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].
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: To find the maximum subset sum in an array.

 
0
  #2
Jul 3rd, 2006
The maximum would be 5 + 10, just toss out all negative values.
Last edited by Ancient Dragon; Jul 3rd, 2006 at 8:42 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: To find the maximum subset sum in an array.

 
0
  #3
Jul 3rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: To find the maximum subset sum in an array.

 
0
  #4
Jul 3rd, 2006
First generate all the subsets, then add the totals.

The one with the biggest total wins?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: To find the maximum subset sum in an array.

 
0
  #5
Jul 3rd, 2006
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.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: To find the maximum subset sum in an array.

 
0
  #6
Jul 3rd, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: To find the maximum subset sum in an array.

 
0
  #7
Jul 3rd, 2006
Originally Posted by iamthwee
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: To find the maximum subset sum in an array.

 
0
  #8
Jul 3rd, 2006
Originally Posted by dilip.mathews
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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: To find the maximum subset sum in an array.

 
0
  #9
Jul 3rd, 2006
Originally Posted by iamthwee
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: To find the maximum subset sum in an array.

 
0
  #10
Jul 3rd, 2006
Originally Posted by dilip.mathews
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.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC