You can solve this without iterating through permutations. Say you are trying to compute f (n). Where can a new, large number go? It must be in the up position, which is an even position. You can have any valid sequence of odd length preceding it, and any valid sequence after it.
Say we compute f (n, k), where the highest val is at position k, the zero index. This is zero for k even. For odd k we get:
f (n, k) = select (n-1, k) * f (k) * f (n - k - 1)
To get f (n), the sum f (n, k) over odd k <n.
We must first calculate the first few.
f(0) = 1 f(1) = 1 f(2) = 1 f(3) = f(3,1) = choose(2,1) * f(1) * f(1) = 2 * 1 *1 = 2 f(4) = f(4,1) + f(4,3) = choose(3,1) * f(1) * f(2) + choose(3,3) * f(3) * f(0) = 3*1*1 + 1*2*1 = 5 f(5) = f(5,1) + f(5,3) = choose(4,1) * f(1) * f(3) + choose(4,3) * f(3) * f(1) = 4*1*2 + 4*2*1 = 16
source share