# Permutations of n things taken all at a time with m things never come together

Given n and m, the task is to find the number of permutations of n distinct things taking them all at a time such that m particular things never come together.

Examples:

```Input  : 7, 3
Output : 420

Input  : 9, 2
Output : 282240
```

Approach:

Derivation of the formula –
Total number of arrangements possible using n distinct objects taking all at a time =
Number of arrangements of n distinct things taking all at a time, when m particular things always come together, is
Hence, the number of permutations of distinct things taking all at a time, when particular things never come together –

`Permutations = n! - (n-m+1)! × m!`

Below is the Python implementation of above approach –

## C++

 `#include ` `using` `namespace` `std; ` ` `  `int` `factorial(``int` `n) ` `{ ` `    ``int` `fact = 1; ` `    ``for` `(``int` `i = 2; i <= n; i++) ` `        ``fact = fact * i ; ` `    ``return` `(fact); ` `} ` ` `  `int` `result(``int` `n, ``int` `m) ` `{ ` `    ``return``(factorial(n) -    ` `           ``factorial(n - m + 1) *  ` `           ``factorial(m)); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``cout(result(5, 3)); ` `} ` ` `  `// This code is contributed by Mohit Kumar `

## Java

 `class` `GFG ` `{ ` `static` `int` `factorial(``int` `n) ` `{ ` `    ``int` `fact = ``1``; ` `    ``for` `(``int` `i = ``2``; i <= n; i++) ` `        ``fact = fact * i ; ` `    ``return` `(fact); ` `} ` ` `  `static` `int` `result(``int` `n, ``int` `m) ` `{ ` `    ``return``(factorial(n) -  ` `           ``factorial(n - m + ``1``) *  ` `           ``factorial(m)); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``System.out.println(result(``5``, ``3``)); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `def` `factorial(n):  ` `    ``fact ``=` `1``;  ` `    ``for` `i ``in` `range``(``2``, n ``+` `1``):  ` `        ``fact ``=` `fact ``*` `i  ` `    ``return` `(fact)  ` ` `  `def` `result(n, m): ` `    ``return``(factorial(n) ``-` `factorial(n ``-` `m ``+` `1``) ``*` `factorial(m)) ` ` `  `# driver code ` `print``(result(``5``, ``3``)) `

## C#

 `using` `System; ` ` `  `class` `GFG  ` `{  ` `    ``static` `int` `factorial(``int` `n)  ` `    ``{  ` `        ``int` `fact = 1;  ` `        ``for` `(``int` `i = 2; i <= n; i++)  ` `            ``fact = fact * i ;  ` `        ``return` `(fact);  ` `    ``}  ` `     `  `    ``static` `int` `result(``int` `n, ``int` `m)  ` `    ``{  ` `        ``return``(factorial(n) -  ` `               ``factorial(n - m + 1) *  ` `               ``factorial(m));  ` `    ``}  ` `     `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``Console.WriteLine(result(5, 3));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output :

`84`

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.