Count the number of special permutations - GeeksforGeeks

11 May, 2022
Given two positive integers n and k, the task is to count the number of special permutations. A special permutation P is defined as a permutation of first n natural numbers in which there exists at least (n – k) indices such that Pi = i. Prerequisite: Derangements
Examples: 
Input: n = 4, k = 2 Output: 7 {1, 2, 3, 4}, {1, 2, 4, 3}, {4, 2, 3, 1}, {2, 1, 3, 4}, {1, 4, 3, 2}, {1, 3, 2, 4} and {3, 2, 1, 4} are the only possible special permutations.
Input: n = 5, k = 3 Output: 31 
Approach: Let the function fx denote the number of special permutations in which there exists exactly x indices such that Pi = i. Hence, the required answer will be:  
f(n – k) + f(n – k + 1) + f(n – k + 2) + ... + f(n – 1) + fn  
Now, fx can be calculated by choosing x indices from n where Pi = i and calculating the number of derangements for (n – i) other indices as for them Pi should not be equal to i then multiplying the result by nCx as there can be different ways to select x indices from n.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count the number// of required permutations#include <bits/stdc++.h>using namespace std;#define ll long long int // Function to return the number of ways// to choose r objects out of n objectsint nCr(int n, int r){    int ans = 1;    if (r > n - r)        r = n - r;    for (int i = 0; i < r; i++) {        ans *= (n - i);        ans /= (i + 1);    }    return ans;} // Function to return the number// of derangements of nint countDerangements(int n){    int der[n + 1];     der[0] = 1;    der[1] = 0;    der[2] = 1;     for (int i = 3; i <= n; i++)        der[i] = (i - 1) * (der[i - 1]                         + der[i - 2]);    return der[n];} // Function to return the required// number of permutationsll countPermutations(int n, int k){    ll ans = 0;    for (int i = n - k; i <= n; i++) {         // Ways to choose i indices from n indices        int ways = nCr(n, i);         // Derangements of (n - i) indices        ans += ways * countDerangements(n - i);    }    return ans;} // Driver Code to test above functionsint main(){    int n = 5, k = 3;    cout << countPermutations(n, k);    return 0;}
// Java program to count the number// of required permutations     public class GFG{         // Function to return the number of ways    // to choose r objects out of n objects    static int nCr(int n, int r)    {        int ans = 1;        if (r > n - r)            r = n - r;        for (int i = 0; i < r; i++) {            ans *= (n - i);            ans /= (i + 1);        }        return ans;    }         // Function to return the number    // of derangements of n    static int countDerangements(int n)    {        int der[] = new int[ n + 3];             der[0] = 1;        der[1] = 0;        der[2] = 1;             for (int i = 3; i <= n; i++)            der[i] = (i - 1) * (der[i - 1]                            + der[i - 2]);        return der[n];    }         // Function to return the required    // number of permutations    static int countPermutations(int n, int k)    {        int ans = 0;        for (int i = n - k; i <= n; i++) {                 // Ways to choose i indices from n indices            int ways = nCr(n, i);                 // Derangements of (n - i) indices            //System.out.println(ans) ;            ans += (ways * countDerangements(n- i));        }        return ans;    }         // Driver Code to test above functions    public static void main(String []args)    {        int n = 5, k = 3;        System.out.println(countPermutations(n, k)) ;    }    // This code is contributed by Ryuga}
# Python 3 program to count the number# of required permutation # function to return the number of ways# to chooser objects out of n objectsdef nCr(n, r):    ans = 1    if r > n - r:        r = n - r    for i in range(r):        ans *= (n - i)        ans /= (i + 1)    return ans # function to return the number of# degrangemnets of ndef countDerangements(n):    der = [0 for i in range(n + 3)]         der[0] = 1    der[1] = 0    der[2] = 1         for i in range(3, n + 1):        der[i] = (i - 1) * (der[i - 1] +                            der[i - 2])             return der[n] # function to return the required# number of permutationsdef countPermutations(n, k):    ans = 0    for i in range(n - k, n + 1):                 # ways to chose i indices        # from n indices        ways = nCr(n, i)                 # Derangements of (n-i) indices        ans += ways * countDerangements(n - i)    return ans # Driver Coden, k = 5, 3 print(countPermutations(n, k)) # This code is contributed by# Mohit kumar 29 (IIIT gwalior)
     // C# program to count the number// of required permutationsusing System;   public class GFG{          // Function to return the number of ways    // to choose r objects out of n objects    static int nCr(int n, int r)    {        int ans = 1;        if (r > n - r)            r = n - r;        for (int i = 0; i < r; i++) {            ans *= (n - i);            ans /= (i + 1);        }        return ans;    }          // Function to return the number    // of derangements of n    static int countDerangements(int n)    {        int []der = new int[ n + 3];              der[0] = 1;        der[1] = 0;        der[2] = 1;              for (int i = 3; i <= n; i++)            der[i] = (i - 1) * (der[i - 1]                            + der[i - 2]);        return der[n];    }          // Function to return the required    // number of permutations    static int countPermutations(int n, int k)    {        int ans = 0;        for (int i = n - k; i <= n; i++) {                  // Ways to choose i indices from n indices            int ways = nCr(n, i);                  // Derangements of (n - i) indices            //System.out.println(ans) ;            ans += (ways * countDerangements(n- i));        }        return ans;    }          // Driver Code to test above functions    public static void Main()    {        int n = 5, k = 3;        Console.WriteLine(countPermutations(n, k)) ;    }}// This code is contributed by 29AjayKumar
<?php// PHP program to count the number// of required permutations // Function to return the number of ways// to choose r objects out of n objectsfunction nCr($n, $r){    $ans = 1;    if ($r > $n - $r)        $r = $n - $r;    for ($i = 0; $i < $r; $i++)    {        $ans *= ($n - $i);        $ans /= ($i + 1);    }    return $ans;} // Function to return the number// of derangements of nfunction countDerangements($n){    $der = array($n + 1);     $der[0] = 1;    $der[1] = 0;    $der[2] = 1;     for ($i = 3; $i <= $n; $i++)        $der[$i] = ($i - 1) *                   ($der[$i - 1] +                    $der[$i - 2]);    return $der[$n];} // Function to return the required// number of permutationsfunction countPermutations($n, $k){    $ans = 0;    for ($i = $n - $k; $i <= $n; $i++)    {         // Ways to choose i indices        // from n indices        $ways = nCr($n, $i);         // Derangements of (n - i) indices        $ans += $ways * countDerangements($n - $i);    }    return $ans;} // Driver Code$n = 5;$k = 3;echo (countPermutations($n, $k)); // This code is contributed// by Shivi_Aggarwal?>
<script>       // JavaScript program to count the number      // of required permutations      // Function to return the number of ways      // to choose r objects out of n objects      function nCr(n, r) {        var ans = 1;        if (r > n - r) r = n - r;        for (var i = 0; i < r; i++) {          ans *= n - i;          ans /= i + 1;        }        return ans;      }       // Function to return the number      // of derangements of n      function countDerangements(n) {        var der = [...Array(n + 1)];         der[0] = 1;        der[1] = 0;        der[2] = 1;         for (var i = 3; i <= n; i++)          der[i] = (i - 1) * (der[i - 1] + der[i - 2]);        return der[n];      }       // Function to return the required      // number of permutations      function countPermutations(n, k) {        var ans = 0;        for (var i = n - k; i <= n; i++) {          // Ways to choose i indices from n indices          var ways = nCr(n, i);           // Derangements of (n - i) indices          ans += ways * countDerangements(n - i);        }        return ans;      }       // Driver Code to test above functions      var n = 5,        k = 3;      document.write(countPermutations(n, k));       </script>
31
 
Time Complexity: O(N^2) Auxiliary Space: O(N)
Shivi_Aggarwal
mohit kumar 29
ankthon
29AjayKumar
ManasChhabra2
rdtank
pankajsharmagfg
surindertarika1234
mitalibhola94
binomial coefficient
Technical Scripter 2018
Algorithms
Combinatorial
Dynamic Programming
Mathematical
Dynamic Programming
Mathematical
Combinatorial
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
How to Start Learning DSA?
Recursive Practice Problems with Solutions
Permutation and Combination in Python
itertools.combinations() module in Python to print all possible combinations
Combinational Sum
Factorial of a large number
Count ways to reach the nth stair using step 1, 2 or 3