Version 2 - print new even-length arrays from two arrays This is an update of the code for my previous post (which has now been removed as this code is more efficient and accurate) for the GeeksForGeeks task.

Task outline

Given two arrays, print all new arrays of even length where each subsequent element in the new array comes alternately from A then B and each element is larger than the previous element

Example:


Array 1 = [1, 2, 3]
Array 2 = [2, 4, 6]


Possible new array lengths = [2, 4, 6]

The output for arrays of length 2 is:
[1,2], [1,4], [1,6], [2,4], [2,6], [3,4] [3,6]

The full solution for the above includes the following two additional arrays:
[1 2 3 4], [1 2 3 6]

This solution uses the itertools library 

import itertools
A=[10, 20, 30,40]
B=[11,21,31,41]
list_solutions = []


for x in range (1,min(len(A),len(B))+1):
    newA = list(itertools.combinations(A,x))
    newB = list(itertools.combinations(B,x))
    for itemA in newA:
        for itemB in newB:
            to_print = True 
            for index in range (min(len(itemA),len(itemB))):
                if itemA[index] &gt;= itemB[index]: 
                    to_print = False 
                    break 
            if to_print == True: 
                list_solutions.append([itemA, itemB])

#Print only valid solutions:
for item in list_solutions:
    print_valid = True 
    for index in range (len(item[0])):
        if item[0][index] &gt;= item[1][index]:
            print_valid = False 
            break 
        if index &gt;= 1:
            if item[0][index] &lt;= item[1][index-1]:
                print_valid = False
                break 
    if print_valid == True:
        for index in range (len(item[0])):
            print (item[0][index], item[1][index], sep = " ", end = " ")
        print ("")
    if print_valid == False:
        continue
It's unclear to me why you didn't chose to use a tree. From your example you know:


A[0] has the children B[0:].
A[1] has the children B[1:].
A[2] has the children B[1:].
B[0] has the children A[2:].
B[1] has no children.
B[2] has no children.


From this you should be able to see that you'll just have to walk the tree to get the values.

To get all the values you walk the tree with the roots being all the values in A. And you filter odd results.

class Node:
    def __init__(self, value):
        self.value = value
        self.children = []

    def walk(self, path=None):
        path = (path or ()) + (self.value,)
        yield path
        for child in self.children:
            yield from child.walk(path)


def solution(A, B):
    A = [Node(a) for a in A]
    B = [Node(b) for b in B]

    for parents, children in ((A, B), (B, A)):
        for parent in parents:
            parent.children = [
                child
                for child in children
                if child.value &gt; parent.value
            ]

    for start in A:
        for values in start.walk():
            if len(values) % 2 == 0:
                print(values)