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 7193751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:14:00+00:00 2026-05-28T20:14:00+00:00

I’m trying to write C++ code which will compute all possible orderings of groups

  • 0

I’m trying to write C++ code which will compute all possible orderings of groups of integers, whose sizes are given in a vector (unknown in length).

For example, if the input is (group1=2, group2=3, group3=2), then I need to compute every ordering of the first 7 integers, in groups of 2, 3, and 2.

Groups matter in avoiding duplicates (i.e. 1234567 is the same as 2134567 which is the same as 1234576).

The output should be:

1234567
1234657
1234756
1235647
...
6734512

But there could be as many as 10 different groups given (usually the size of the group is less than or equal to 5)

I’m still fairly new to C++, but it seems like I need to use next_permutation from algorithm, possibly in recursion, but I haven’t been able to work out the details.

The following is working (inefficient) R code I’ve written, (in case my question was unclear) but it’s far too slow for reasonably large group numbers/sizes.

Thanks!

#Input group sizes for k;
CombnMult<-function(k){
n<-sum(k)
A1<-combn(n,k[1])
CHOOSE<-rep(0,length(k))
CHOOSE[1]<-factorial(n)/(factorial(k[1])*factorial(n-k[1]))
for(i in 1:(length(k)-1)){
CHOOSE[i+1]<-factorial(n-cumsum(k)[i])/(factorial(k[i+1])*factorial(n-cumsum(k)[i+1]))
assign(paste("B",i,sep=''),matrix(get(paste("A",i,sep='')), nrow=cumsum(k)[i]))
if(length((1:n)[is.na(pmatch((1:n),get(paste("B",i,sep=''))[,1]))])>1)  
assign(paste("A",i+1,sep=''),apply(get(paste("B",i,sep='')),2,function(z) rbind(matrix(rep(z,CHOOSE[i+1]),ncol=CHOOSE[i+1]),combn((1:n)[is.na(pmatch((1:n),z))],k[i+1]))))
if(length((1:n)[is.na(pmatch((1:n),get(paste("B",i,sep=''))[,1]))])==1) 
assign(paste("A",i+1,sep=''),apply(get(paste("B",i,sep='')),2,function(z) rbind(matrix(rep(z,CHOOSE[i+1]),ncol=CHOOSE[i+1]),(1:n)[is.na(pmatch((1:n),z))])))
}
t(get(paste("A",i+1,sep='')))
}

#Example:
CombnMult(c(2,3,2))
  • 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-28T20:14:01+00:00Added an answer on May 28, 2026 at 8:14 pm

    This seems to work for me:

    void allocate_to_groups_impl( std::vector<char>& usage, std::vector<int>& order, const std::vector<int>& cumsum, int group, int place, int min )
    {
        if (place == cumsum[group]) {
            group++;
            min = 0;
            if (group == cumsum.size()) {
                // the allocation is stored in order, we'll just print it out but you could do something else with it
                for( std::vector<int>::iterator it = order.begin(); it != order.end(); ++it )
                    putchar(digitset[*it]);
                putchar('\n');
                return;
            }
        }
    
        for( int v = min, max = usage.size() + place - cumsum[group]; v <= max; ++v ) {
            if (!usage[v]) {
                order[place] = v;
                usage[v] = 1;
                allocate_to_groups_impl(usage, order, cumsum, group, place+1, v+1);
                usage[v] = 0;
            }
        }
    }
    
    template<size_t Ngroups>
    void allocate_to_groups( int (&c)[Ngroups] )
    {
        size_t sum_of_c = 0;
        std::vector<int> cumsum_of_c;
        for( int* it = c; it < c + Ngroups; ++it )
            cumsum_of_c.push_back(sum_of_c += *it);
        std::vector<int> order(sum_of_c);
        std::vector<char> usage(sum_of_c);
    
        allocate_to_groups_impl(usage, order, cumsum_of_c, 0, 0, 0);
    }
    
    • C++11 demo: http://ideone.com/HUy02
    • C++03 demo: http://ideone.com/P0QlJ

    How it works: A fairly standard enumeration of all permutations of the identifiers, modified so that the identifiers within a single group are forced to be strictly increasing. At each group boundary, the identifier is allowed to decrease.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

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.