I thought about the same problem and ended up writing a small Prolog program for creating permutations and “discovered” the relationship to Catalan numbers, and then found your question. So this is not the answer to your question, but here is the Prolog program:
% Generate permutation counts count_pushpop(NK) :- length(_, N), findall(Seq, pushpop(N, Seq), Seqs), length(Seqs, K). % Create an integer sequence from 1 to N % and permutate it using all possible push-pop % operations starting with an empty stack. pushpop(N, Seq) :- numlist(1, N, List), pushpop(List, [], Seq). % Generate all the possible ways a list % of items can be pushed into a stack % and poped out of it. pushpop([], [], []). pushpop([H | List], Stack, Seq) :- pushpop(List, [H | Stack], Seq). pushpop(List, [H | Stack], [H | Seq]) :- pushpop(List, Stack, Seq).
Proof that not all permutations of n! :
?- findall(Seq, pushpop(3, Seq), Seqs). Seqs = [[3, 2, 1], [2, 3, 1], [2, 1, 3], [1, 3, 2], [1, 2, 3]].
Proof that it generates Catalan numbers (or it would be proof if it weren't for the stack overflow;)):
?- count_pushpop(NK). N = K, K = 0 ; N = K, K = 1 ; N = K, K = 2 ; N = 3, K = 5 ; N = 4, K = 14 ; N = 5, K = 42 ; N = 6, K = 132 ; N = 7, K = 429 ; N = 8, K = 1430 ; N = 9, K = 4862 ; N = 10, K = 16796 ; N = 11, K = 58786 ; N = 12, K = 208012 ; ERROR: Out of global stack