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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum sum by picking elements from two arrays in order
- Maximum sum by picking elements from two arrays in order | Set 2
- 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
- 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
- Find Array formed by adding each element of given array with largest element in new array to its left
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Iterating over all possible combinations in an Array using Bits
- Iterative approach to print all combinations of an Array
- Largest element smaller than current element on left for every element in Array
- Find just strictly greater element from first array for each element in second array
- Array formed from difference of each element from the largest element in the given array
- Form an array of distinct elements with each element as sum of an element from each 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
- Find three element from different three arrays such that a + b + c = sum