In how many ways can I merge $m$ and $n$ items without disturbing the order in each group?

872 Views Asked by At

I have two lists having all distinct elements. One contains $m$ elements and other contains $n$ elements. We need to arrange them such that the order of elements of individual lists is not disturbed.

Example -
for list1=[1,2], list2=[3,4]
m=2,n=2
Total ways to arrange them will be -
1-2-3-4
1-3-2-4
1-3-4-2
3-4-1-2
3-1-2-4
3-1-4-2

I am unable to come up with a general solution. PS: This is not a homework/assignment.

2

There are 2 best solutions below

4
On BEST ANSWER

Hint: You know how many total slots there are in the final list. Just pick some set of those slots to receive the items of the first list and everything is determined.

0
On

Ross's answer is perfect already (and in my opinion should be accepted as the correct answer) - it is the right way of thinking about this question and offers insight to the problem.

However, I just thought I'd add a rather general scattergun way of tackling these sorts of problems, where you are counting possibilities subject to symmetric constraints.

It doesn't always give a particularly simplified (or for that matter, insightful) answer, but is often a useful alternative approach.

The idea is as follows:

  • First, count how many possibilities there are without any constraints.

  • Secondly, divide by the number of times we've counted each target

In this case, we can consider the total possibilities as being all the $(m+n)!$ total orderings of the $m+n$ items. However, we only want to count target orderings where each of the $m$ items in list 1 and $n$ items from list 2 are in the original order.

Observe though, that we can match each target ordering with $m! \cdot n!$ total orderings by putting the $n$ items from list 1 in arbitrary order, and the $m$ items from list 2 in another arbitrary order.

Ie, the target possibility 1,2,3,4 is represented by the overall possibilities 1,2,3,4; 2,1,3,4; 1,2,4,3 and 2,1,4,3

Hence, to get the number of target possibilities, we divide to get: $\frac{(m+n)!}{m! \cdot n!} = \binom{m+n}{m}$.

Apologies, I haven't explained this very well, but hopefully it makes some sense.