Find the number of good permutations - GeeksforGeeks

20 May, 2021
Given two integers N and K. The task is to find the number of good permutations of the first N natural numbers. A permutation is called good if there exist at least N – K indices i (1 ≤ i ≤ N) such that Pi = 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 Pi does not equal 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 Pi not equals to i – there are nCm ways to do this, then we need to construct a permutation Q for chosen indices such that for every chosen index Qi is not equaled to i. Permutations with this property are called derangements and the number of derangements of fixed size can be calculated using an exhaustive search for m ≤ 4.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of good permutationsint 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 codeint main(){    int n = 5, k = 2;    cout << Permutations(n, k);     return 0;}
// Java implementation of the approachclass GFG{ // Function to return the count of good permutationsstatic 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 codepublic static void main(String[] args){    int n = 5, k = 2;    System.out.println(Permutations(n, k));}} // This code contributed by Rajput-Ji
# Python3 implementation of the approach # Function to return the count# of good permutationsdef 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 codeif __name__ == "__main__":     n, k = 5, 2    print(Permutations(n, k))     # This code is contributed# by Rituraj Jain
// C# implementation of the above approach.using System; class GFG{ // Function to return the count of good permutationsstatic 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 codepublic static void Main(){    int n = 5, k = 2;    Console.WriteLine(Permutations(n, k));}} /* This code contributed by PrinciRaj1992 */
<?php// PHP implementation of the approach // Function to return the count// of good permutationsfunction 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.?>
<script> // JavaScript implementation of the approach // Function to return the count of good permutationsfunction Permutations(n, k){         // For m = 0, ans is 1    var 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 Codevar n = 5, k = 2;document.write(Permutations(n, k));   // This code is contributed by Khushboogoyal499 </script>
11
 
rituraj_jain
Rajput-Ji
princiraj1992
Code_Mech
khushboogoyal499
Numbers
permutation
Combinatorial
Mathematical
Mathematical
Numbers
permutation
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Combinational Sum
Lexicographic rank of a string
Count of subsets with sum equal to X
Print all permutations in sorted (lexicographic) order
Print all possible strings of length k that can be formed from a set of n characters
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Program to find GCD or HCF of two numbers