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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:16:42+00:00 2026-05-19T01:16:42+00:00

Folks, I have N bounded sets: S1 = {s11, s12, … s1a } S2

  • 0

Folks,
I have N bounded sets:

S1 = {s11, s12, ... s1a }
S2 = {s21, s22, ... s2b }
...
sN=  {sN1, sN2, ... sNx }

I have a function f() that takes one argument A from each set:

f( A1, A2, ... AN ) such that Ax belongs to Sx

I need to invoke f() for all possible combinations of arguments:

f( s11, s21, ... sN1 )
f( s11, s21, ... sN2 )
f( s11, s21, ... sN3 )
...
f( s11, s21, ... sNx )
...
f( s1a, s2b, ... sNx )

Can someone help me figure out a recursive (or iterative) algorithm that will hit all combinations?

Thanks in advance.

-Raj

  • 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-19T01:16:42+00:00Added an answer on May 19, 2026 at 1:16 am

    So basically you want to generate the cartesian product s1 x s2 x ... x sN.

    This is a classic application of backtracking / recursion. Here’s how a pseudocode would look like:

    function CartesianProduct(current, k)
        if (k == N + 1)
            current is one possibility, so call f(current[1], current[2], ..., current[N])
            and return
    
        for each element e in Sk
            call CartesianProduct(current + {e}, k + 1)
    
    Initial call is CartesianProduct({}, 1)
    

    You should write it on paper and see how it works. For example, consider the sets:

    s1 = {1, 2}
    s2 = {3, 4}
    s3 = {5, 6}
    

    The first call will be CartesianProduct({}, 1), which will then start iterating over the elements in the first set. The first recursive call with thus be CartesianProduct({1}, 2). This will go on in the same manner, eventually reaching CartesianProduct({1, 3, 5}, 4), for which the termination condition will be true (current.Length == N + 1). Then it will backtrack and call CartesianProduct({1, 3, 6}, 4) and so on, until all possibilities are generated. Run it on paper all the way to see exactly how it works.
    A

    Extra credit: can you figure out how to get rid of the k parameter?

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

Sidebar

Related Questions

Curious whether folks have setup 2 way transactional replication on the tables ASP.NET uses
I have seen several places in stackoverflow where folks have elegantly and easily turned
Folks I have a sealed class as follows. I want to extend this sealed
hi folks i have a subroutine called CheckDate() in the code behind. How would
folks! I have project in cc.net and this project nay start by 3 ways
Folks, We have been using OpenXml APIs found in System.IO.Packaging for creating a package
Folks, I have a problem. I am a writing a script in python which
Folks I am surprised to see my code working, that I dont have any
So I have this function that forks N number of child processes. However it
I have defined a function in vim to properly indent folds. Ie so they

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.