Search code examples
arrayspermutation

Total number of different permutations of an array in which relative order of elements in two disjoint sub-arrays remain constant


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:

  1. {1, 2, 20, 19}
  2. {1, 20, 2, 19}
  3. {1, 20, 19, 2}
  4. {20, 1, 19, 2}
  5. {20, 19, 1, 2}

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.


Solution

    1. In your example it will be 6 permutations. You've missed the {20,1,2,19}.

    2. 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