Different ways to arrange a set of numbers, so X can be seen from the left and Y can be seen from the right

218 Views Asked by At

Given an set of unique integers of length N. What are number of different ways you can rearrange the array so that, you can only see X numbers of integers from the left and Y numbers of integers from the right, with the restriction being that the biggr integer in the set blocks the smaller integers. So if you had say [10,40,60,20], you would see 3 from the left and 2 from the right.

Example:

Given: N = 3, X = 2, Y = 2 Output: 2

Given: N = 6, X = 1, Y = 2 Output: 24

1

There are 1 best solutions below

0
On BEST ANSWER

You get a tractable recurrence if, instead of considering where the largest number is, you look at the smallest number.

The indexing will seem strange here, but bear with me. Let $f(n,l,r)$ denote the number of permutations of $\{0,\ldots,n\}$ where $l+1$ elements are visible from the left and $r+1$ are visible from the right. Clearly $f(n,l,r)=0$ if either $l$ or $r$ is negative, since at least one element (the largest) is visible on each side. We have $f(0,l,r)=[l=r=0]$. Also, the only way the smallest element is visible is if it is at one end, so we get the recurrence $$f(n,l,r)=f(n-1,l-1,r)+f(n-1,l,r-1)+(n-1)f(n-1,l,r)$$ for $n>1$.

It's not hard to show inductively that $$f(n,l,r)=\binom{l+r}{r}\,|S_n^{(l+r)}|$$ where $S_n^{(k)}$ denotes the Stirling number of the first kind. (There's probably a simple direct argument but I haven't had time to think about that.)

In particular, the two given examples are $\binom21\,|S_2^{(2)}|=2$ and $\binom10\,|S_5^{(1)}|=24$.