Bobby Teja
BAN USER- 0of 0 votes
AnswersGiven 'n' no of sets and each set is of a variable size. Find out the cross product of these 'n' of sets.
- Bobby Teja
For eg.
{1,2} , {3,4} , {5,6,7} these are the three sets.The cross product of these sets would be as below.
{1,3,5},{1,3,6},{1,3,7},{1,4,5},{1,4,6},{1,4,7},{2,3,5},{2,3,6},{2,3,7},{2,4,5},{2,4,6},{2,4,7}.
My interviewer asked me to write the code for this problem in C .| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm
Can someone tel me how to solve this problem in C? Please provide me a better algo...
- Bobby Teja February 18, 2011@Towhid
How will you find out the unique character? Can you please elaborate your algorithm
@king kong
Dude so r u going to use 'n' no of for loops if there are 'n' sets.. I have done the program this way during my interview but my interviewer told me that it was not an efficient way. here is my code below.
#include <iostream>
using namespace std;
int main()
{
int array1[] = {1,2};
int array2[] = {3,4};
int array3[] = {5,6,7};
int i,j,k;
printf("The final cross product of my lists is below\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<3;k++)
{
printf("%d %d %d",array1[i],array2[j],array3[k]);
printf("\n");
}
}
}
return 0;
}
Hello people the answer would be n^2 times to get the same order as it was in the starting.. I can explain you with an example.
- Bobby Teja February 20, 2011Take numbers 1,2,3,4,5 .
As per the question,
1st step- First card should be placed on the desk.
2nd step- Immediate next card should be kept at the bottom of the stack.
0th iteration - 1,2,3,4,5
1st iteration - 3,4,5,2 | 1
2nd iteration - 5,2,4 | 3,1
3rd iteration - 4,2 | 5,3,1
4th iteration - 2 | 4,5,3,1
5th iteration - 2,4,5,3,1
6th iteration - 5,3,1,4 | 2
7th iteration - 1,4,3 | 5,2
8th iteration - 3,4 | 1,5,2
9th iteration - 4 | 3,1,5,2
10th iteration - 4,3,1,5,2
11th iteration - 1,5,2,3 |4
12th iteration - 2,3,5 | 1,4
13th iteration - 5,3 | 2,1,4
14th iteration - 3 | 5,2,1,4
15th iteration - 3,5,2,1,4
16th iteration - 2,1,4,5 | 3
17th iteration - 4,5,1 | 2,3
18th iteration - 1,5 | 4,2,3
19th iteration - 5 | 1,4,2,3
20th iteration - 5,1,4,2,3
21st iteration - 4,2,3,1 | 5
22nd iteration - 3,1,2 | 4,5
23rd iteration - 2,1 | 3,4,5
24th iteration - 1 | 2,3,4,5
25th iteration - 1,2,3,4,5
So, in the same way it takes 313*313 = 97969 iterations to get the same pack of cards which were present initially.