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

  • Home
  • SEARCH
  • 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 588171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:17:26+00:00 2026-05-13T15:17:26+00:00

I am scratching my head trying to do this and it’s eating me up.

  • 0

I am scratching my head trying to do this and it’s eating me up. I know it is not THAT complex. I have a number of items, this number can be equal or greater than three. Then I need to determine the possible combination of group of items that will complete the total. The only restriction it’s that the groups should have three or more items, not exceeding (but including) seven items.

For example:

If I have 7 items, then I could have these possible groups:

  • 1 group of 7 items.
  • 1 group of 4 items and 1 group of 3 items.

If I have 12 items, I could have these possible groups:

  • 4 groups of 3 items.
  • 3 groups of 4 items.
  • 2 groups of 6 items.
  • 1 group of 7 items + 1 group of 5 items.
  • 2 groups of 3 and 1 group of 6 items.
  • 1 group of 3, 1 group of 4 and 1 group of five items.
  • …

I thought about recursion and started implementing the algorithm. It’s obviously not working. I suck at recursion. A lot.

//Instance Fields
public List<ArrayList<String>> options;

//Method that will generate the options. The different options are 
//stored in a list of "option". An individual option will store a list of
//strings with the individual groups.
public void generateOptions(int items, ArrayList<String> currentOption){

    //If the current option is null, then create a new option.
    if(currentOption == null){
        currentOption = new ArrayList<String>();
    }
    if(items < 3){
        //If the number of items is less than three then it doesn't comply with the 
        //requirements (teams should be more or equal than three. 
        currentOption.add("1 group of "+items+" items");
        options.add(currentOption);
    }
    else{
        //I can make groups of 3,4,5,6 and 7 items.
        for(int i = 3;i<=7;i++){
            if(items%i == 0){ 
                // If the number of items is divisible per the current number, 
                // then a possible option could be items/i groups of i items. 
                // Example: Items = 9. A possible option is 3 groups of 3 items.
                currentOption.add(items/i +" groups of "+ i+" items");
                options.add(currentOption);
            }
            else{
                // If the number of items - the current number is equal or greater than
                // three, then a possible option could be a group of i items
                // and then I'll have items-i items to separate in other groups.
                if(items - i >=3){
                    currentOption.add("1 group of "+i+" items");
                    generateOptions(items-i,currentOption);
                }
            }
        }
    }
}

Thanks for your help!!!

  • 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-13T15:17:26+00:00Added an answer on May 13, 2026 at 3:17 pm

    Here’s an algorithm (expressed in C++) to solve a more general version of the problem,
    with arbitrary upper and lower bounds on the addends that may appear in each partition:

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    typedef vector<int> Partition;
    typedef vector<Partition> Partition_list;
    
    // Count and return all partitions of an integer N using only 
    // addends between min and max inclusive.
    
    int p(int min, int max, int n, Partition_list &v)
    {
       if (min > max) return 0;
       if (min > n) return 0;     
       if (min == n) {
          Partition vtemp(1,min);
          v.push_back(vtemp);
          return 1;
       }
       else {
         Partition_list part1,part2;
         int p1 = p(min+1,max,n,part1);
         int p2 = p(min,max,n-min,part2);
         v.insert(v.end(),part1.begin(),part1.end());
         for(int i=0; i < p2; i++)
         {
            part2[i].push_back(min);
         }
         v.insert(v.end(),part2.begin(),part2.end());
         return p1+p2;
       }
    }
    
    void print_partition(Partition &p)
    {
       for(int i=0; i < p.size(); i++) {
          cout << p[i] << ' ';
       }
       cout << "\n";
    }
    
    void print_partition_list(Partition_list &pl)
    {
       for(int i = 0; i < pl.size(); i++) {
          print_partition(pl[i]);
       }
    }
    
    int main(int argc, char **argv)
    {
       Partition_list v_master;
    
       int n = atoi(argv[1]);
       int min = atoi(argv[2]);
       int max = atoi(argv[3]);
       int count = p(min,max,n,v_master);
       cout << count << " partitions of " << n << " with min " << min  ;
       cout << " and max " << max << ":\n" ;
       print_partition_list(v_master);
    }
    

    And some sample output:

    $ ./partitions 12 3 7              
    6 partitions of 12 with min 3 and max 7:
    6 6 
    7 5 
    4 4 4 
    5 4 3 
    6 3 3 
    3 3 3 3 
    $ ./partitions 50 10 20            
    38 partitions of 50 with min 10 and max 20:
    17 17 16 
    18 16 16 
    18 17 15 
    19 16 15 
    20 15 15 
    18 18 14 
    19 17 14 
    20 16 14 
    19 18 13 
    20 17 13 
    19 19 12 
    20 18 12 
    13 13 12 12 
    14 12 12 12 
    20 19 11 
    13 13 13 11 
    14 13 12 11 
    15 12 12 11 
    14 14 11 11 
    15 13 11 11 
    16 12 11 11 
    17 11 11 11 
    20 20 10 
    14 13 13 10 
    14 14 12 10 
    15 13 12 10 
    16 12 12 10 
    15 14 11 10 
    16 13 11 10 
    17 12 11 10 
    18 11 11 10 
    15 15 10 10 
    16 14 10 10 
    17 13 10 10 
    18 12 10 10 
    19 11 10 10 
    20 10 10 10 
    10 10 10 10 10 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am scratching my head with this one. I am trying to run an
I'm scratching my head on this one. I'm trying to use boost::variant to store
I have been scratching my head on this one for a while, so I
I've been scratching my head on this one for a while now and am
This one has me scratching my head. I'm running Subversion 1.3.1 (r19032) on Ubuntu.
I was kind of scratching my head at this a week ago, and now
This may seem like a somewhat contrived example, but I'm left scratching my head.
Alright, this one (3a; sample problem with provided answer) has got me scratching my
I've been scratching my head for a good hour or so now, but I
I am trying to work my way around the ObsoleteElementError http://bit.ly/qhEhMv and scratching my

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.