Given two integers **N** and **K**. The task is to find the number of good permutations of first **N** natural numbers. A permutation is called good if there exist at least **N – K** indices **i** (1 ≤ i ≤ N) such that **P _{i} = i**.

**Examples:**

Input:N = 4, K = 1

Output:1

{1, 2, 3, 4} is the only possible good permutation.

Input:N = 5, K = 2

Output:11

**Approach:** Let’s iterate on **m** which is the number of indices such that **P _{i}** not equals to

**i**. Obviously,

**0 ≤ m ≤ k**.

In order to count the number of permutations with fixed

**m**, we need to choose the indices that have the property

**P**not equals to

_{i}**i**– there are

**ways to do this then we need to construct a permutation**

^{n}C_{m}**Q**for chosen indices such that for every chosen index

**Q**not equals to

_{i}**i**. Permutations with this property are called derangements and the number of derangements of fixed size can be calculated using exhaustive search since

**m ≤ 4**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of good permutations ` `int` `Permutations(` `int` `n, ` `int` `k) ` `{ ` ` ` `// For m = 0, ans is 1 ` ` ` `int` `ans = 1; ` ` ` ` ` `// If k is greater than 1 ` ` ` `if` `(k >= 2) ` ` ` `ans += (n) * (n - 1) / 2; ` ` ` ` ` `// If k is greater than 2 ` ` ` `if` `(k >= 3) ` ` ` `ans += (n) * (n - 1) * (n - 2) * 2 / 6; ` ` ` ` ` `// If k is greater than 3 ` ` ` `if` `(k >= 4) ` ` ` `ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, k = 2; ` ` ` `cout << Permutations(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count of good permutations ` `static` `int` `Permutations(` `int` `n, ` `int` `k) ` `{ ` ` ` `// For m = 0, ans is 1 ` ` ` `int` `ans = ` `1` `; ` ` ` ` ` `// If k is greater than 1 ` ` ` `if` `(k >= ` `2` `) ` ` ` `ans += (n) * (n - ` `1` `) / ` `2` `; ` ` ` ` ` `// If k is greater than 2 ` ` ` `if` `(k >= ` `3` `) ` ` ` `ans += (n) * (n - ` `1` `) * (n - ` `2` `) * ` `2` `/ ` `6` `; ` ` ` ` ` `// If k is greater than 3 ` ` ` `if` `(k >= ` `4` `) ` ` ` `ans += (n) * (n - ` `1` `) * (n - ` `2` `) * (n - ` `3` `) * ` `9` `/ ` `24` `; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `, k = ` `2` `; ` ` ` `System.out.println(Permutations(n, k)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of good permutations ` `def` `Permutations(n, k): ` ` ` ` ` `# For m = 0, ans is 1 ` ` ` `ans ` `=` `1` ` ` ` ` `# If k is greater than 1 ` ` ` `if` `k >` `=` `2` `: ` ` ` `ans ` `+` `=` `(n) ` `*` `(n ` `-` `1` `) ` `/` `/` `2` ` ` ` ` `# If k is greater than 2 ` ` ` `if` `k >` `=` `3` `: ` ` ` `ans ` `+` `=` `((n) ` `*` `(n ` `-` `1` `) ` `*` ` ` `(n ` `-` `2` `) ` `*` `2` `/` `/` `6` `) ` ` ` ` ` `# If k is greater than 3 ` ` ` `if` `k >` `=` `4` `: ` ` ` `ans ` `+` `=` `((n) ` `*` `(n ` `-` `1` `) ` `*` `(n ` `-` `2` `) ` `*` ` ` `(n ` `-` `3` `) ` `*` `9` `/` `/` `24` `) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n, k ` `=` `5` `, ` `2` ` ` `print` `(Permutations(n, k)) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of good permutations ` `static` `int` `Permutations(` `int` `n, ` `int` `k) ` `{ ` ` ` `// For m = 0, ans is 1 ` ` ` `int` `ans = 1; ` ` ` ` ` `// If k is greater than 1 ` ` ` `if` `(k >= 2) ` ` ` `ans += (n) * (n - 1) / 2; ` ` ` ` ` `// If k is greater than 2 ` ` ` `if` `(k >= 3) ` ` ` `ans += (n) * (n - 1) * (n - 2) * 2 / 6; ` ` ` ` ` `// If k is greater than 3 ` ` ` `if` `(k >= 4) ` ` ` `ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 5, k = 2; ` ` ` `Console.WriteLine(Permutations(n, k)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count ` `// of good permutations ` `function` `Permutations(` `$n` `, ` `$k` `) ` `{ ` ` ` `// For m = 0, ans is 1 ` ` ` `$ans` `= 1; ` ` ` ` ` `// If k is greater than 1 ` ` ` `if` `(` `$k` `>= 2) ` ` ` `$ans` `+= (` `$n` `) * (` `$n` `- 1) / 2; ` ` ` ` ` `// If k is greater than 2 ` ` ` `if` `(` `$k` `>= 3) ` ` ` `$ans` `+= (` `$n` `) * (` `$n` `- 1) * ` ` ` `(` `$n` `- 2) * 2 / 6; ` ` ` ` ` `// If k is greater than 3 ` ` ` `if` `(` `$k` `>= 4) ` ` ` `$ans` `+= (` `$n` `) * (` `$n` `- 1) * (` `$n` `- 2) * ` ` ` `(` `$n` `- 3) * 9 / 24; ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$n` `= 5; ` `$k` `= 2; ` `echo` `(Permutations(` `$n` `, ` `$k` `)); ` ` ` `// This code contributed by Code_Mech. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

11

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the largest good number in the divisors of given number N
- Find the minimum number of elements that should be removed to make an array good
- Find the Number of Permutations that satisfy the given condition in an array
- Find all good indices in the given Array
- Find the good permutation of first N natural numbers
- Find the shortest distance between any pair of two different good nodes
- Check whether a number is Good prime or not
- Minimum number of elements that should be removed to make the array good
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Number of palindromic permutations | Set 1
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Generate all possible permutations of a Number divisible by N
- Count the number of special permutations
- Generate all cyclic permutations of a number
- Print all permutations of a number N greater than itself
- Smallest number containing all possible N length permutations using digits 0 to D
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Maximize a number considering permutations with values smaller than limit
- Number of unique permutations starting with 1 of a Binary String

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.