Print combination of arrays

Given the following arrays
[a1, a2, a3, a4]
[b1, b2, b3]
[c1, c2]
[d1, d2]

Print all the combination. The order does not matter,for example:  [a1, b2] [b2, a1] are considered the same. Do it without nested loop

Example:
[a1, b1, c1, d1]
[a1, b1, c1, d2]
[a1, b1, c2, d1]
[a1, b1, c2, d2]

C++:
01 void printArrayCombination(vector <int*> A, int S[4], int start, vector <int> B)
02 {
03     if(start==4){
04         for(int i=0;i<4;i++)
05             cout<<B[i]<<” “;
06         cout<<endl;
07         return;
08     }
09     for(int i=0;i<S[start];i++){
10         B.push_back(A[start][i]);
11         printArrayCombination(A,S,start+1,B);
12         B.pop_back();
13     } 
14}
Advertisements
This entry was posted in Array and linked list, Backtrack. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s