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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:14:56+00:00 2026-05-23T23:14:56+00:00

there are n arrays a_0, …, a_n-1 , each of l elements. how to

  • 0

there are n arrays a_0, ..., a_n-1, each of l elements. how to write efficient code to iterate all combinations, in which each element is picked from a distinct array. for example, if two arrays are [0, 1] and [3, 4], then the output should be

[0, 3]
[0, 4]
[1, 3]
[1, 4]
  • 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-23T23:14:56+00:00Added an answer on May 23, 2026 at 11:14 pm

    You cannot do better than O(l^n), as nightcracker correctly points out. Here’s one way that you can approach the problem.

    Make a large array A whose ith entry is the array a_i, i.e.

    A[i] = a_i

    Now iterate through all words of length n on the alphabet {0,1,...,l}:

    -(array*)nextWord:(array*)word {
        array *newWord = word;
        for (int i=n-1; i=>0; ++i) {
            if (word[i] < l) {
                newWord[i] = word[i]+1;
                for (int j=i+1; j<n; ++j) {
                    newWord[i] = 0;
                }
                return newWord;
            }
        }
        return NULL;
    }
    

    Finally, select the entries based on the word by

    word = [0, 0, ... , 0];
    while (word != NULL) {
        A[0][word[0]], A[1][word[1]], ... , A[n-1][word[n-1]];
        word = nextWord(word);
    }
    

    Sorry for the inconsistencies in the pseudocode, but hopefully you can discern the logic here.


    Incidentally, based on the example in the question, I am assuming that the first entry should come from the first array, the second entry from the second array, and so on. If this is not the case, then you can still use the idea above and then permute the entries. However, doing this may lead to repetitions if and only if two of the arrays have a common entry.

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

Sidebar

Related Questions

Is there anyway to vectorize the addition of elements across arrays in a SIMD
Input is a boolean array a_0,i with at most 1000,000 elements. each time the
I have two arrays: a = [1,2,3] b = [1,4,3] Is there an element-wise
I want to store short arrays in a field. (I realize there are reasons
Is there a best (see below) way to append two byte arrays in C#?
Is there a way to create two dimensional NSArray without nesting arrays in the
Is there a built-in function for PHP for me to check whether two arrays
I have two arrays : name[] and roll[] Is there a way to traverse
Is there an easy way to check in a unit test that two arrays
If I have two byte[] arrays, is there a built-in function to compare them

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.