Algorithm for all subsets of Array

You can build a backtracking based solution to formulate all the possible sub-sequence for the given array. Sharing an ideone link for the same:

all = []

def gen(A, idx = 0, cur = []):
    if idx >= len(A):
        if len(cur): all.append(cur)

    gen(A, idx + 1, list(cur))
    incl = list(cur)
    gen(A, idx + 1, incl)

def solve(A):
    global all
    all = []
    return all

print(solve([1, 4, 6]))

Browse More Popular Posts

Leave a Comment