# How to calculate the number of ways to insert 'r' distinct items into 'n' distinct items?

Say, $$n=5$$ $$[1,2,3,4,5]$$,

$$r=1$$ .

The the ways to insert "$$r=1$$"$$[x]$$ element(s) into $$5$$-items are as follows:-

Way-1:- $$[x,1,2,3,4,5]$$

Way-2:- $$[1,x,2,3,4,5]$$

Way-3:- $$[1,2,x,3,4,5]$$

Way-4:- $$[1,2,3,x,4,5]$$

Way-5:- $$[1,2,3,4,x,5]$$

Way-6:- $$[1,2,3,4,5,x]$$

Another example, $$say$$, $$n=3,$$ $$[1,2,3]$$

$$r=2[x,y]$$

The ways are as follows :-

Way-1:- $$[x,y,1,2,3]$$

Way-2:- $$[1,x,y,2,3]$$

Way-3:- $$[1,2,x,y,3]$$

Way-4:- $$[1,2,3,x,y]$$

Way-5:- $$[x,1,y,2,3]$$

Way-6:- $$[x,1,2,y,3]$$

Way-7:- $$[x,1,2,3,y]$$

Way-8:- $$[1,x,2,y,3]$$

Way-9:- $$[1,x,2,3,y]$$

Way-10:-$$[1,2,x,3,y]$$

Total number of ways :- $$10$$.

The main question here is, for any given $$n$$ and $$r$$ , is there are a formula using combinatorics which can give the total number of ways ? Edit:- The $$'n'$$ items stay in the same order forever. Also, the '$$r$$' items always stay in order . First comes '$$x$$' , then only comes '$$y$$

• Welcome to Mathematics stack exchange. Are you familiar with stars and bars? Commented Jan 20, 2020 at 14:53
• Yes, Think of $n+r$ slots. There are ${n+r\choose r}$ ways of selecting $r$ slots to insert the (presumed indistinguishable items).The $n$ distinguishable items go into the remaining slots in a particular order. If you want all possible orders for the $n$ items, then multiply by $n!$. The "stars and bars" concept is closely related but slightly different. In that case the $r$ items represent "dividers" to divide the $n$ items into $r+1$ groups. Commented Jan 20, 2020 at 14:56
• The 'n' items stay in the same order forever, so that's not a problem I guess :-) Also, the 'r' items always stay in order . First comes 'x' , then only comes 'y'. Commented Jan 20, 2020 at 14:59
• probably a typo given a qwerty keyboard ...
– user645636
Commented Jan 20, 2020 at 15:00
• $n+1$ for one item. the sum of $t+1$ for all $t<n$ if two items. etc.
– user645636
Commented Jan 20, 2020 at 15:06

Why are not you considering arrangements of $$x$$ and $$y$$ in the 2nd example ($$y$$ can come before $$x$$)
For $$n$$ elements we have $$(n+1)$$ gaps, lets say $$x_1$$ elements goes to gap 1, $$x_2$$ elements in gap 2 .... And so on.
we have $$x_1 + x_2+ \cdots + x_{n+1} = r$$ ( where all $$x$$'s are whole numbers) , the number of possibilities is $${n+r \choose r}$$ (also known as beggar's method dealing with providing $$n$$ coins to $$r$$ beggars).
And now finally arranging all $$r$$ things which is $$r!$$
So the final answer $${n+r \choose r}\times r!$$