wu :: forums
« wu :: forums - Longest SubSequence with Sum <= K »

Welcome, Guest. Please Login or Register.
May 16th, 2024, 6:56am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: towr, william wu, SMQ, Eigenray, ThudnBlunder, Icarus, Grimbal)
   Longest SubSequence with Sum <= K
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Longest SubSequence with Sum <= K  (Read 6222 times)
sateesp
Newbie
*





   


Gender: male
Posts: 35
Longest SubSequence with Sum <= K  
« on: Sep 4th, 2010, 12:20pm »
Quote Quote Modify Modify

Given an array, find the longest sub set whose sum is less or equal then the given MaxSum
int[] FindMaxSumArray(int[] array, int maxsum)
for example, given array: {1, -2, 4, 5, -2, 6, 7}
maxsum=7
The result would be: {1,-2, -2, 6}
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Longest SubSequence with Sum <= K  
« Reply #1 on: Sep 4th, 2010, 1:08pm »
Quote Quote Modify Modify

Sort the array, and the take the smallest numbers that sum to less than or equal the maxsum.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Longest SubSequence with Sum <= K  
« Reply #2 on: Sep 8th, 2010, 5:13am »
Quote Quote Modify Modify

The *longest* subset?
Wouldn't {1, -2, 4, 5, -2} (sum 6) be longer?
IP Logged
birbal
Full Member
***





   


Gender: male
Posts: 250
Re: Longest SubSequence with Sum <= K  
« Reply #3 on: Sep 11th, 2010, 2:04am »
Quote Quote Modify Modify

on Sep 8th, 2010, 5:13am, Grimbal wrote:
The *longest* subset?
Wouldn't {1, -2, 4, 5, -2} (sum 6) be longer?

Yes, i think {1, -2, 4, 5, -2} this should be the set.
IP Logged

The only thing we have to fear is fear itself!
MrLambert
Newbie
*





   


Posts: 13
Re: Longest SubSequence with Sum <= K  
« Reply #4 on: Oct 31st, 2011, 9:40am »
Quote Quote Modify Modify

Yes, its a mistake
set should be: {1, -2, 4, 5, -2}
IP Logged
computing
Newbie
*





   


Gender: male
Posts: 2
Re: Longest SubSequence with Sum <= K  
« Reply #5 on: Nov 7th, 2011, 2:01am »
Quote Quote Modify Modify

One simple way is to use Kadane's algorithm and keep verifying input constraints, in such a way that the sum_so_far should be less than MaxSum, if so, consider next element. Otherwise, current indices [i, j] of sum_so_far will be answer. A DP solution.
 
We may need to repeat the procedure for all those sub array sizes that are less than current sub array that satisfies input constraints. I mean, we also need to check the remaining array [j+1, n], if (n - j) > (j - i + 1).
 
Kadane algorithm - http://en.wikipedia.org/wiki/Maximum_subarray_problem
 
Any thoughts?
« Last Edit: Nov 7th, 2011, 2:02am by computing » IP Logged
kaka
Newbie
*





   


Gender: male
Posts: 24
Re: Longest SubSequence with Sum <= K  
« Reply #6 on: Nov 28th, 2011, 11:40pm »
Quote Quote Modify Modify

@computing: Kadane's algorithm is about finding the contiguous subarray where as this one is about sub set (order doesn't matter)
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board