Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4086266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:42:07+00:00 2026-05-20T18:42:07+00:00

I am working on a set partitioning problem and need a way to define

  • 0

I am working on a set partitioning problem and need a way to define all combinations of unordered bucket sizes. Given N elements and exactly M groups, find every combination of group sizes such that the sum of the group sizes is N. Note: The size of the bucket cannot be 0.

For example, assume 6 items need to be placed in 3 buckets. The solution I’m looking for is:

([1,2,3],[1,1,4],[2,2,2])

To map these equally, I use a map function as follows:

@grouping = map { int( ($items + $_) / $groups ) } 0 .. $groups-1;

To get all combinations I’m thinking some kind of recursive function where each level of recursion N finds the possible values for element N in the array. The eligible values each level can insert is >= previousLevel. This is sort of what I’m thinking but there has got to be a better way to do this….

sub getList($$@){
    my $itemCount = shift;
    my $groupCount = shift;
    my @currentArray = @_;
    my $positionToFill= @currentArray;
    if($positionToFill == 0){
        my $minValue = 1;
    }
    else{
        my $minValue = currentArray[$positionToFill-1];
    }
    my $currentSum = sum(@currentArray);
    return undef if $currentSum + $minValue >= $items;

    my @possibleCombinations = ();
    for(my $i = $minValue; $i < $items - $currentSum; $i++){
        $currentArray[$positionToFill] = $i;
        if($positionToFill == $groupCount-1){
            push(@possibleCombinations, \@currentArray)
        }
        else{
            push(@possibleCombinations, getList($itemCount, $groupCount, @currentArray);
        }                        
    }
    return @currentArray;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:42:08+00:00Added an answer on May 20, 2026 at 6:42 pm

    To group N items into M groups, ultimately you need a recursive function that groups N-1 (or fewer) items into M-1 groups.

    sub partition {
        # @results is a list of array references, the part of the partitions
        # created in previous iterations
        my ($N, $M, @results) = @_;
    
        if ($M == 1) {
            # only one group. All elements must go in this group.
            return map [ sort {$a <=> $b} @$_, $N ], @results;
        }
    
        # otherwise, put from 1 to $N/$M items in the next group,
        # and invoke this function recursively
        my @new_results = ();
        for (my $n = 1; $n <= $N/$M; $n++) {
            push @new_results, partition($N-$n, $M-1,
                                    map [ @$_, $n ] @results);
        }
        return @new_results;
    }
    

    and start the process with a call like

    @all_partitions = partition(6, 3, []);    #  [] = list with one ref to an empty array
    

    This method will produce a few duplicates that you’ll have to filter out, but overall it will be pretty efficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a problem with set not working in a batch file; it took
Whilst writing working my way through a list of scripts I need to write
How to define a working set of lexer and parser ( exempli gratia: flex
How do I calculate the private working set of memory using C#? I'm interested
I would like to display some memory statistics (working set, GCs etc.) on a
Where can I find the option to switch between the Windows Working Set in
I've got my php working to set a cookie storing the http_referer the visitor
I'm working on a set of classes that will be used to serialize to
I am working with a set of data that looks something like the following.
I am working with a set of data that I have converted to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.