I am having a problem: There are several sets of numbers and I need to find all combinations of them where I choose just one value from each set. First element from the first set, second element from the second set, etc. For example:
Set1 - {1, 2, 3}
Set2 - {4, 5, 6, 7}
Set3 - {8, 9, 10}
The result should be:
1, 4, 8
1, 4, 9
1, 4, 10
1, 5, 8
1, 5, 9
1, 5, 10
...
3, 7, 9
3, 7, 10
I have written a simple recursive algorithm. My algorithm is much like the accepted answers of Howto create combinations of several vectors without hardcoding loops in C++? and generate all combination of elements in 2d vector.
But this solution is not effective for large sets. It requires about size1*size2*size3
complexity to get the result and for larger sets (more than 100 elements) it starts to consume a lot of CPU resources. Is there any other method or approach for finding all the combinations, that will work faster and will be optimal for larger sets?
P.S. I have written my algorithm in C++, but any language is welcomed for examples.
EDIT :
I need to find all those combinations that satisfy a specific condition, which looks like something (A>1) && (B>2) && (C>3)
. Which means that if at least one value from set A is more than 1, and at least one value from set B is more than 2, and at least one value from set C is more than 3.