You have found an interesting pattern:
0, 1, 3, 2, 6, 7, 5, 4
This, written in binary, is
000, 001, 011, 010, 110, 111, 101, 100
What's so fascinating about this pattern is that adjacent binary forms (and the tail with the head) differ by exactly one bit.
Let's denote such a pattern array with length 2^n (n a positive integer) that starts from 0 as P(n).
You found out that P(n) may be constructed as the following:
If n = 1, then P(n) = [0, 1]
Otherwise, P(n) = P(n - 1) + h(n, reverse(P(n - 1))),
where reverse(a) is a function that reverses the order of the array a,
and h(n, a) is a function that sets the n'th lowest bit of each element in a.
For example, you could construct P(3) by the following:
P(2) = [00, 01, 11, 10]
reverse(P(2)) = [10, 11, 01, 00]
h(3, reverse(P(2))) = [110, 111, 101, 100]
P(3) = P(2) + h(3, reverse(P(2))) = [00, 01, 11, 10] + [110, 111, 101, 100]
= [00, 01, 11, 10, 110, 111, 101, 100]
= [0, 1, 3, 2, 6, 7, 5, 4]
Now, write a program that constructs P(n).
An integer n (0 < n <= 18) followed by a newline character.
P(n), each element separated by a space (but no space at the end), and a trailing newline character.