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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:19:56+00:00 2026-06-03T04:19:56+00:00

I have an array of n elements, I need to put all 2- combination

  • 0

I have an array of n elements, I need to put all 2- combination of them into arrays of length 2. for example:

suppose comb is a 2 dimensional array.

n = 1,2,3

I need to put all 2- combinations to comb[i][j] like that:

comb[0][0] = {1}
comb[0][1] = {2}

comb[1][0] = {1}
comb[1][1] = {3}

comb[2][0] = {2}
comb[2][1] = {3}  

I do not know how to write the code!
Thanks

My Answer:

The O(n!) answer: n = total number m= total possible answer

int m = 0;
for (int i = 0; i < n - 1; i++){
    int first = a[i];
    for(int j = i+1 ; j < n ; j++){
        int second = a[j];
        comb[m][0] = first;
        comb[m][1] = second;
        ++m;
}

}

  • 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-03T04:19:57+00:00Added an answer on June 3, 2026 at 4:19 am

    One easy way is using the next_permutation function available in the STL library in order to generate all the possible permutations of your numbers, and then pick the first two elements of each one. Note that the sequence must be first sorted, as if not the previous permutations will be skipped.

    int nums[] = {1, 2, 3, 4};
    while (next_permutation(nums, nums + 4)) {
        cout << nums[0] << ", " << nums[1] << endl;
    }
    

    Remember that you must #include <algorithm> to use this function.

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

Sidebar

Related Questions

I have an array of elements in Ruby [2,4,6,3,8] I need to remove elements
I have an array which i need to sort its elements by occurrence then
I have an array Items which has 10 elements. I need to remove the
I have 16 1D arrays with approximately 10-11 million double-precision elements each. I need
I have a NSMutableArray and I need to sort its elements into separate C
I have put few elements in array (e.g. 5 elements) The first element, index[0]
I have an array of CheckboxField[] elements that I need to dynamically initialize. My
Consider I have an array of elements out of which I want to create
VISUAL C++ Question Hi, I have array of 3 elements and I want to
I have an array with 16 elements. I would like to evaluate these 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.