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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:49:06+00:00 2026-06-04T19:49:06+00:00

I’m interested in performing a Cartesian product on n arrays. I can write the

  • 0

I’m interested in performing a Cartesian product on n arrays. I can write the code if I know the number of arrays ahead of time. For example, given 2 arrays:

int[] a = new int[]{1,2,3};
int[] b = new int[]{1,2,3};

for(int i=0; i<=a.length; i++){
    for(int j=0; j<=b.length; j++){
        System.out.println(a[i]*b[j]);
    }
}

The problem is that at runtime, I do not know the number of arrays. I may have 2 arrays, or I may have 100 arrays. Is there a way that I can do this? Thanks!

  • 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-04T19:49:08+00:00Added an answer on June 4, 2026 at 7:49 pm

    One way to approach this problem is to continuously reduce the number of arrays one at a time by noting that

    A0 × A1 × A2 = (A0 × A1) × A2

    Consequently, you could write a function like this one, which computes the Cartesian product of two arrays:

    int[] cartesianProduct(int[] one, int[] two) {
        int[] result = new int[one.length * two.length];
        int index = 0;
    
        for (int v1: one) {
            for (int v2: two) {
                result[index] = v1 * v2;
                index++;
            }
        }
    
        return result;
    }
    

    Now, you can use this function to keep combining together pairs of arrays into one single array containing the overall Cartesian product. In pseudocode:

    While there is more than one array left:
        Remove two arrays.
        Compute their Cartesian product.
        Add that array back into the list.
    Output the last array.
    

    And, as actual Java:

    Queue<int[]> worklist;
    /* fill the worklist with your arrays; error if there are no arrays. */
    
    while (worklist.size() > 1) {
        int[] first = worklist.remove();
        int[] second = worklist.remove();
        worklist.add(cartesianProduct(first, second));
    }
    
    /* Obtain the result. */
    int[] result = worklist.remove();
    

    The problem with this approach is that it uses memory proportional to the total number of elements you produce. This can be a really huge number! If you just want to print all of the values out one at a time without storing them, there is a more efficient approach. The idea is that you can start listing off all possible combinations of indices in the different arrays, then just go multiply together the values at those positions. One way to do this is to maintain an “index array” saying what the next index to look at is. You can move from one index to the next by “incrementing” the array the same way you would increment a number. Here’s some code for that:

    int[] indexArray = new int[arrays.length];
    mainLoop: while (true) {
        /* Compute this entry. */
        int result = 1;
        for (int i = 0; i < arrays.length; i++) {
            result *= arrays[i][indexArray[i]]
        }
        System.out.println(result);
    
        /* Increment the index array. */
        int index = 0;
        while (true) {
            /* See if we can bump this array index to the next value.  If so, great!
             * We're done.
             */
            indexArray[index]++;
            if (indexArray[index] < arrays[i].length) break;
    
            /* Otherwise, overflow has occurred.  If this is the very last array, we're
             * done.
             */
            indexArray[index] = 0;
            index ++;
    
            if (index == indexArray.length) break mainLoop;
        }
    }
    

    This uses only O(L) memory, where L is the number of arrays you have, but produces potentially exponentially many values.

    Hope this helps!

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
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 got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.