Give an array of integers.
For example a = {1,2,20,19} Let two disjoint sub-arrays be {1,2} and {20,19}. There are 5 such permutations in which '1' always comes before '2' and '20' always comes before '19' and such are:
My question is:
Given an array, a[1...n+m] of size=n+m. Find the number of permutations in which relative order of elements in two sub-arrays a[1..n] and a[n+1..n+m] remains the same.
In your example it will be 6 permutations. You've missed the {20,1,2,19}.
It is a pure math(combinatorics) question. The question is equivalent to -
find the number of all possible solutions of the equation :
x0+x1+....+xn = m
The answer is defined by the formula - (n+m)!/(m!n!)
For example in your case it will be n=m=2
and the answer is 4!/2!2! = 6