Langford Sequence - Utilize symmetry / Remove symmetry
L(s, n)
is "up to reversal of the order" see e.g. https://oeis.org/A014552 .
This means e.g. that for |L(2, 4)|
we have
4 1 3 1 2 4 3 2
and
2 3 4 2 1 3 1 4
are both satisfying the property, but one is just the reverse of the other so |L(2, 4)| = 1
.
To take this into account in your algorithm, you can check e.g. at the very first level that there are more free bits to the left than to the right.
NB: your algorithm enumerates all solutions, so the complexity is > L(2, n)
and for n = 20
this is already more than 2^41
. You probably won't reach this. As mentionned on the Wikipedia page:
for large
n
the number of solutions can be calculated more efficiently by algebraic methods