Given a list of arrays, find all combinations where each combination contains one element from each given array.
Input : [ [1, 2], [3, 4] ] Output : 1 3 1 4 2 3 2 4 Input : [ , [2, 3, 4],  ] Output : 1 2 5 1 3 5 1 4 5
We keep an array of size equal to total no of arrays. This array called indices helps us keep track of the index of current element in each of the n arrays. Initially it is initialized with all 0s indicating current index in each array is that of first element. We keep printing the combinations until no new combinations can be found. Starting from the rightmost array we check if more elements are there in that array. If yes, we increment the entry for that array in indices i.e. move to the next element in that array. We also make the current indices 0 in all the arrays to the right of this array. We keep moving left to check all arrays until one such array is found. If no more arrays are found we stop there.
1 4 5 1 4 6 2 4 5 2 4 6 3 4 5 3 4 6
This article is contributed by aditi sharma 2. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Maximum sum by picking elements from two arrays in order
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Maximum area rectangle by picking four sides from array
- K maximum sum combinations from two arrays
- Digits of element wise sum of two arrays into a new array
- itertools.combinations() module in Python to print all possible combinations
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Iterating over all possible combinations in an Array using Bits
- Iterative approach to print all combinations of an Array
- Array formed from difference of each element from the largest element in the given array
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Closest greater element for every array element from another array
- Replace each element by the difference of the total size of the array and frequency of that element
- Range Query on array whose each element is XOR of index value and previous element
- Find last element after deleting every second element in array of n integers