Discarding the required number of largest or smallest is the only way to achieve your "efficiency" definition. This takes a second or two for random lists of 100,000 (by avoiding recalculating the average):

#! perl -slw use strict; use List::Util qw[ sum ]; use Math::Random::MT qw[ rand srand ]; our $MAX //= 2**24; our $N //= 1e5; our $S //= 1; our $TARGET //= 5e6; srand( $S ); my $nameRoot = 'fileAAAAA'; my %files = map { $nameRoot++ => int( rand $MAX ) } 1 .. $N; my @namesByValue = sort{ $files{ $a } <=> $files{ $b } } keys %files; my @orderedValues = @files{ @namesByValue }; my $ave = sum( @orderedValues ) / @orderedValues ; print "Original average: ", $ave; my @namesToDiscard; if( $ave > $TARGET ) { ## Loose some big ones. while( $ave > ( $TARGET + 500 ) ) { push @namesToDiscard, pop @namesByValue; my $change = ( pop( @orderedValues ) - $ave ) / @orderedValues +; $ave -= $change; } } else { ## loose some small ones; while( $ave < ( $TARGET + 500 ) ) { push @namesToDiscard, shift @namesByValue; my $change = ( $ave - shift @orderedValues ) / @orderedValues +; $ave += $change; } } printf "TARGET: $TARGET; Achieved ave:$ave (using %d files)\n", scalar @orderedValues; delete @files{ @namesToDiscard }; __END__ c:\test>858276 -TARGET=5e6 Original average: 8374785.96135 TARGET: 5e6; Achieved ave:5000496.12061175 (using 59762 files) c:\test>858276 -TARGET=10e6 Original average: 8374785.96135 TARGET: 10e6; Achieved ave:10000541.8121829 (using 80621 files) c:\test>858276 -TARGET=7.5e6 Original average: 8374785.96135 TARGET: 7.5e6; Achieved ave:7500462.85185227 (using 89593 files)

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re: Creating a sub-list constrained by an average while maintaining maximum number of original elements by BrowserUk
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.