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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum sum by picking elements from two arrays in order | Set 2
- 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
- K maximum sum combinations from two arrays
- Maximum area rectangle by picking four sides from array
- Digits of element wise sum of two arrays into a new array
- Minimum capacity of small arrays needed to contain all element of the given 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
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Find Array formed by adding each element of given array with largest element in new array to its left
- Iterative approach to print all combinations of an Array
- Largest element smaller than current element on left for every element in Array
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Form an array of distinct elements with each element as sum of an element from each array
- Array formed from difference of each element from the largest element in the given array
- Find just strictly greater element from first array for each element in second array
- Closest greater element for every array element from another array
- Find three element from given three arrays such that their sum is X | Set 2