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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:49:46+00:00 2026-05-30T12:49:46+00:00

I have been searching for a source code for generating combinations using C++. I

  • 0

I have been searching for a source code for generating combinations using C++. I found some advanced codes for this but that is good for only specific number predefined data. Can anyone give me some hints, or perhaps, some ideas to generate a combination?

As an example, suppose the set S = { 1, 2, 3, ...., n} and we pick r= 2 out of it. The input would be n and r. In this case, the program will generate arrays of length two. So input of 5 2 would output 1 2, 1 3.

I had difficulty in constructing the algorithm. It took me a month to think about this.

  • 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-30T12:49:48+00:00Added an answer on May 30, 2026 at 12:49 pm

    A simple way using std::next_permutation:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    int main() {
        int n, r;
        std::cin >> n;
        std::cin >> r;
    
        std::vector<bool> v(n);
        std::fill(v.end() - r, v.end(), true);
    
        do {
            for (int i = 0; i < n; ++i) {
                if (v[i]) {
                    std::cout << (i + 1) << " ";
                }
            }
            std::cout << "\n";
        } while (std::next_permutation(v.begin(), v.end()));
        return 0;
    }
    

    or a slight variation that outputs the results in an easier to follow order:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    int main() {
       int n, r;
       std::cin >> n;
       std::cin >> r;
    
       std::vector<bool> v(n);
       std::fill(v.begin(), v.begin() + r, true);
    
       do {
           for (int i = 0; i < n; ++i) {
               if (v[i]) {
                   std::cout << (i + 1) << " ";
               }
           }
           std::cout << "\n";
       } while (std::prev_permutation(v.begin(), v.end()));
       return 0;
    }
    

    A bit of explanation:

    It works by creating a "selection array" (v), where we place r selectors, then we create all permutations of these selectors, and print the corresponding set member if it is selected in in the current permutation of v.

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

Sidebar

Related Questions

I have been searching around using Google but I can't find an answer to
I have been searching to get the source code of the header file <graphics.h>
I have been searching for info on this to no avail. The context of
I have been searching for a way to insert linebreaks in my code for
I have been searching forever. Sorry, I am pretty desperate at this point so
I have been searching everywhere but I could not find an answer. I need
So i have been searching the web for a javascript source for an Hmac-sha1
I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in
I have been searching for a solution for this issue for multiple hours today
I've been looking to read some ruby code(specifically Rails) but I don't want to

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.