Jack B. Nymbol,
Here is a greedy approach that I think would produce good results (though I haven't even attempted to test it) and should be relatively fast.
  1. Divide the list into two ordered sublists (above and below the target average)
  2. Take the largest item off the above list
  3. Take as many items off the below list (smallest first) until you are within the target average
  4. Take the smallest item off the below list
  5. Take as many items off the above list (smallest first) until you are within the target average
  6. If either steps 2/3 or 4/5 fail to balance, drop the item from step 2/4 and repeat the process
  7. Repeat until one of the two lists is empty
  8. Of the list that remains, see if any of the remaining items closest to the target can be added to the pile and maintain the target average

Cheers - L~R


In reply to Re: Creating a sub-list constrained by an average while maintaining maximum number of original elements by Limbic~Region
in thread Creating a sub-list constrained by an average while maintaining maximum number of original elements by Jack B. Nymbol

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.