A sequence of positive real numbers S1, S2, S3, …, SN is called a superincreasing sequence if every element of the sequence is greater than the sum of all the previous elements in the sequence. E.g: 1, 3, 6, 13, 27, 52.
Given a sorted list A, I want to iterate over all combinations of A, which are superincreasing.
How example:
A = [28, 34, 44, 60, 71, 150, 167, 212, 230, 239, 415, 431, 434, 559, 688]
Valid examples of subsequences:
34, 44, 239, 434 34, 212, 434, 688 Here is a simple brute force example:
def is_superincreasing(seq): total = 0 test = True for n in seq: if n <= total: test = False break total += n return test def combinations(A): N = len(A) for i in range(2**N): combo = [] for j in range(N): if (i >> j) % 2 == 1: combo.append(A[j]) if is_superincreasing(combo): yield combo I'd like to know if there is a better algorithm than O(2^n).