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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:20:13+00:00 2026-06-11T16:20:13+00:00

So, I was searching for a good solution for my problem. I need to

  • 0

So, I was searching for a good solution for my problem.

I need to generate(print) all the combination of a list of integers, for example:
if the array contain integers from 0 to n-1, where n = 5:

int array[] = {0,1,2,3,4};

the order of integers in the combination are NOT important, meaning {1,1,3}, {1,3,1} and {3,1,1} are actually the same combination because they all contain one 3 and two ones.

so for the above array, all combination of length 3:

0,0,0 -> the 1st combination
0,0,1
0,0,2
0,0,3
0,0,4
0,1,1 -> this combination is 0,1,1, not 0,1,0 because we already have 0,0,1. 
0,1,2
0,1,3
0,1,4
0,2,2 -> this combination is 0,2,2, not 0,2,0 because we already have 0,0,2. 
0,2,3
.
.
0,4,4
1,1,1 -> this combination is 1,1,1, not 1,0,0 because we already have 0,0,1. 
1,1,2
1,1,3
1,1,4
1,2,2 -> this combination is 1,2,2, not 1,2,0 because we already have 0,1,2.
.
.
4,4,4 -> Last combination

For Now I Wrote Code for doing this, but my problem is:
if the numbers in the array are not integer from 0 to n-1, lets say if the array was like this

int array[] = {1,3,6,7};

my code doesn’t work on this case, any algorithm or code for solving this problem,,

Here is my code :

unsigned int next_combination(unsigned int *ar, int n, unsigned int k){
    unsigned int finished = 0;
    unsigned int changed = 0;
    unsigned int i;

    for (i = k - 1; !finished && !changed; i--) {
        if (ar[i] < n - 1) {
            /* Increment this element */
            ar[i]++;
            if (i < k - 1) {
                /* Make the elements after it the same */
                unsigned int j;
                for (j = i + 1; j < k; j++) {
                    ar[j] = ar[j - 1];
                }
            }
            changed = 1;
        }
        finished = i == 0;
    }
    if (!changed) {
        /* Reset to first combination */
        for (i = 0; i < k; i++){
            ar[i] = 0;
        }
    }
    return changed;
}

And this is the main:

int main(){
    unsigned int numbers[] = {0, 0, 0, 0, 0};
    const unsigned int k = 3;
    unsigned int n = 5;

    do{
        for(int i=0 ; i<k ; ++i)
            cout << numbers[i] << " ";
        cout << endl;
    }while (next_combination(numbers, n, k));

    return 0;
}
  • 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-06-11T16:20:14+00:00Added an answer on June 11, 2026 at 4:20 pm

    If you have working code to generate all combinations of numbers from 0 to n-1, then this is very simple. You have your array of numbers:

    int array[] = {1,3,6,7};
    

    Now, take n = 4, because there are 4 items in the array. Generate all combinations from 0 to 3, and use those as indices into your array. You now have all combinations of your array values by using all combinations of indices into that array.

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

Sidebar

Related Questions

I'm searching for a good solution to create a subset from a existing set.
I'm searching for a good solution to read a zip file from an url
Good evening ladies and gentlemen! Searching for a cross-browser solution to embed commercial fonts
Possible Duplicate: Subclassing UIView to draw in Quartz Hello i am searching good example
I have been searching high and low for a solution to my problem and
I have bees searching for a good solution for Tree Data Structure in Rails
I've been searching for a good way in which to style up a selectbox,
as the title implies i am searching for a good pattern / schema collection
Despite a good deal of searching here I haven't been able to find the
Searching for values that uses the same value Example SELECT Name, UnitPrice, Quantity, Color

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.