Combinations from n arrays picking one element from each array
Last Updated :
03 Mar, 2024
Given a list of arrays, find all combinations where each combination contains one element from each given array.
Examples:
Input : [ [1, 2], [3, 4] ]
Output : 1 3
1 4
2 3
2 4
Input : [ [1], [2, 3, 4], [5] ]
Output : 1 2 5
1 3 5
1 4 5
We keep an array of size equal to the total no of arrays. This array called indices helps us keep track of the index of the current element in each of the n arrays. Initially, it is initialized with all 0s indicating the current index in each array is that of the 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. moves 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector< int > >& arr)
{
int n = arr.size();
int * indices = new int [n];
for ( int i = 0; i < n; i++)
indices[i] = 0;
while (1) {
for ( int i = 0; i < n; i++)
cout << arr[i][indices[i]] << " " ;
cout << endl;
int next = n - 1;
while (next >= 0 &&
(indices[next] + 1 >= arr[next].size()))
next--;
if (next < 0)
return ;
indices[next]++;
for ( int i = next + 1; i < n; i++)
indices[i] = 0;
}
}
int main()
{
vector<vector< int > > arr(3, vector< int >(0, 0));
arr[0].push_back(1);
arr[0].push_back(2);
arr[0].push_back(3);
arr[1].push_back(4);
arr[2].push_back(5);
arr[2].push_back(6);
print(arr);
}
|
Java
import java.util.*;
class GFG{
static void print(Vector<Integer> []arr)
{
int n = arr.length;
int []indices = new int [n];
for ( int i = 0 ; i < n; i++)
indices[i] = 0 ;
while ( true )
{
for ( int i = 0 ; i < n; i++)
System.out.print(
arr[i].get(indices[i]) + " " );
System.out.println();
int next = n - 1 ;
while (next >= 0 &&
(indices[next] + 1 >=
arr[next].size()))
next--;
if (next < 0 )
return ;
indices[next]++;
for ( int i = next + 1 ; i < n; i++)
indices[i] = 0 ;
}
}
public static void main(String[] args)
{
@SuppressWarnings ( "unchecked" )
Vector<Integer> []arr = new Vector[ 3 ];
for ( int i = 0 ; i < arr.length; i++)
arr[i] = new Vector<Integer>();
arr[ 0 ].add( 1 );
arr[ 0 ].add( 2 );
arr[ 0 ].add( 3 );
arr[ 1 ].add( 4 );
arr[ 2 ].add( 5 );
arr[ 2 ].add( 6 );
print(arr);
}
}
|
Python3
def print1(arr):
n = len (arr)
indices = [ 0 for i in range (n)]
while ( 1 ):
for i in range (n):
print (arr[i][indices[i]], end = " " )
print ()
next = n - 1
while ( next > = 0 and
(indices[ next ] + 1 > = len (arr[ next ]))):
next - = 1
if ( next < 0 ):
return
indices[ next ] + = 1
for i in range ( next + 1 , n):
indices[i] = 0
arr = [[] for i in range ( 3 )]
arr[ 0 ].append( 1 )
arr[ 0 ].append( 2 )
arr[ 0 ].append( 3 )
arr[ 1 ].append( 4 )
arr[ 2 ].append( 5 )
arr[ 2 ].append( 6 )
print1(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void print(List< int > []arr)
{
int n = arr.Length;
int []indices = new int [n];
for ( int i = 0; i < n; i++)
indices[i] = 0;
while ( true )
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i][indices[i]] + " " );
Console.WriteLine();
int next = n - 1;
while (next >= 0 &&
(indices[next] + 1 >=
arr[next].Count))
next--;
if (next < 0)
return ;
indices[next]++;
for ( int i = next + 1; i < n; i++)
indices[i] = 0;
}
}
public static void Main(String[] args)
{
List< int > []arr = new List< int >[3];
for ( int i = 0; i < arr.Length; i++)
arr[i] = new List< int >();
arr[0].Add(1);
arr[0].Add(2);
arr[0].Add(3);
arr[1].Add(4);
arr[2].Add(5);
arr[2].Add(6);
print(arr);
}
}
|
Javascript
<script>
function print(arr)
{
let n = arr.length;
let indices = new Array(n);
for (let i = 0; i < n; i++)
indices[i] = 0;
while ( true )
{
for (let i = 0; i < n; i++)
document.write(
arr[i][indices[i]] + " " );
document.write( "<br>" );
let next = n - 1;
while (next >= 0 && (indices[next] + 1 >=
arr[next].length))
next--;
if (next < 0)
return ;
indices[next]++;
for (let i = next + 1; i < n; i++)
indices[i] = 0;
}
}
let arr = new Array(3);
for (let i = 0; i < arr.length; i++)
arr[i] = [];
arr[0].push(1);
arr[0].push(2);
arr[0].push(3);
arr[1].push(4);
arr[2].push(5);
arr[2].push(6);
print(arr);
</script>
|
Output
1 4 5
1 4 6
2 4 5
2 4 6
3 4 5
3 4 6
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...