Count Derangements (Permutation such that no element appears in its original position) - GeeksforGeeks

13 Mar, 2022
A Derangement is a permutation of n elements, such that no element appears in its original position. For example, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0}.Given a number n, find the total number of Derangements of a set of n elements.
Examples : 
Input: n = 2
Output: 1
For two elements say {0, 1}, there is only one 
possible derangement {1, 0}

Input: n = 3
Output: 2
For three elements say {0, 1, 2}, there are two 
possible derangements {2, 0, 1} and {1, 2, 0}

Input: n = 4
Output: 9
For four elements say {0, 1, 2, 3}, there are 9
possible derangements {1, 0, 3, 2} {1, 2, 3, 0}
{1, 3, 0, 2}, {2, 3, 0, 1}, {2, 0, 3, 1}, {2, 3,
1, 0}, {3, 0, 1, 2}, {3, 2, 0, 1} and {3, 2, 1, 0}
Let countDer(n) be count of derangements for n elements. Below is the recursive relation to it.  
countDer(n) = (n - 1) * [countDer(n - 1) + countDer(n - 2)]
How does above recursive relation work? 
There are n – 1 way for element 0 (this explains multiplication with n – 1). Let 0 be placed at index i. There are now two possibilities, depending on whether or not element i is placed at 0 in return. 
i is placed at 0: This case is equivalent to solving the problem for n-2 elements as two elements have just swapped their positions.i is not placed at 0: This case is equivalent to solving the problem for n-1 elements as now there are n-1 elements, n-1 positions and every element has n-2 choices
i is placed at 0: This case is equivalent to solving the problem for n-2 elements as two elements have just swapped their positions.
i is not placed at 0: This case is equivalent to solving the problem for n-1 elements as now there are n-1 elements, n-1 positions and every element has n-2 choices
Below is the simple solution based on the above recursive formula:
C++
Java
Python3
C#
PHP
Javascript
// A Naive Recursive C++ program// to count derangements#include <bits/stdc++.h>using namespace std; int countDer(int n){  // Base cases  if (n == 1) return 0;  if (n == 2) return 1;   // countDer(n) = (n-1)[countDer(n-1) + der(n-2)]  return (n - 1) * (countDer(n - 1) + countDer(n - 2));} // Driver Codeint main(){    int n = 4;    cout << "Count of Derangements is "         << countDer(n);    return 0;}
// A Naive Recursive java// program to count derangementsimport java.io.*; class GFG{         // Function to count    // derangements    static int countDer(int n)    {        // Base cases        if (n == 1) return 0;        if (n == 2) return 1;                 // countDer(n) = (n-1)[countDer(n-1) + der(n-2)]        return (n - 1) * (countDer(n - 1) +                          countDer(n - 2));    }         // Driver Code    public static void main (String[] args)    {        int n = 4;        System.out.println( "Count of Derangements is "                             +countDer(n));     }} // This code is contributed by vt_m
# A Naive Recursive Python3# program to count derangements def countDer(n):         # Base cases    if (n == 1): return 0    if (n == 2): return 1         # countDer(n) = (n-1)[countDer(n-1) + der(n-2)]    return (n - 1) * (countDer(n - 1) +                      countDer(n - 2)) # Driver Coden = 4print("Count of Derangements is ", countDer(n))  # This code is contributed by Azkia Anam.
// A Naive Recursive C#// program to count derangementsusing System; class GFG{         // Function to count    // derangements    static int countDer(int n)    {        // Base cases        if (n == 1) return 0;        if (n == 2) return 1;                 // countDer(n) = (n-1)[countDer(n-1) + der(n-2)]        return (n - 1) * (countDer(n - 1) +                          countDer(n - 2));    }         // Driver Code    public static void Main ()    {        int n = 4;        Console.Write( "Count of Derangements is " +                        countDer(n));     }} // This code is contributed by nitin mittal.
<?php// A Naive Recursive PHP program// to count derangements function countDer($n){         // Base cases    if ($n == 1)        return 0;    if ($n == 2)        return 1;         // countDer(n) = (n-1)[countDer(n-1) +    // der(n-2)]    return ($n - 1) * (countDer($n - 1) +                       countDer($n - 2));}     // Driver Code    $n = 4;    echo "Count of Derangements is ", countDer($n); // This code is contributed by nitin mittal.?>
<script>     // A Naive Recursive Javascript    // program to count derangements         // Function to count    // derangements    function countDer(n)    {        // Base cases        if (n == 1) return 0;        if (n == 2) return 1;                  // countDer(n) = (n-1)[countDer(n-1) + der(n-2)]        return (n - 1) * (countDer(n - 1) + countDer(n - 2));    }         let n = 4;    document.write("Count of Derangements is " + countDer(n));     </script>
Count of Derangements is 9
Time Complexity: O(2^n) since T(n) = T(n-1) + T(n-2) which is exponential.
Auxiliary Space: O(h) where h= log n is the maximum height of the tree.
We can observe that this implementation does repetitive work. For example, see recursion tree for countDer(5), countDer(3) is being evaluated twice. 
cdr() ==> countDer()

                    cdr(5)   
                 /         \     
             cdr(4)          cdr(3)   
           /      \         /     \
       cdr(3)     cdr(2)  cdr(2)   cdr(1)
An Efficient Solution is to use Dynamic Programming to store results of subproblems in an array and build the array in bottom-up manner. 
C++
Java
Python3
C#
PHP
Javascript
// A Dynamic programming based C++// program to count derangements#include <bits/stdc++.h>using namespace std; int countDer(int n){    // Create an array to store    // counts for subproblems    int der[n + 1] = {0};     // Base cases    der[1] = 0;    der[2] = 1;     // Fill der[0..n] in bottom up manner    // using above recursive formula    for (int i = 3; i <= n; ++i)        der[i] = (i - 1) * (der[i - 1] +                            der[i - 2]);     // Return result for n    return der[n];} // Driver codeint main(){    int n = 4;    cout << "Count of Derangements is "         << countDer(n);    return 0;}
// A Dynamic programming based// java program to count derangementsimport java.io.*; class GFG{         // Function to count    // derangements    static int countDer(int n)    {        // Create an array to store        // counts for subproblems        int der[] = new int[n + 1];             // Base cases        der[1] = 0;        der[2] = 1;             // Fill der[0..n] in bottom up        // manner using above recursive        // formula        for (int i = 3; i <= n; ++i)            der[i] = (i - 1) * (der[i - 1] +                                der[i - 2]);             // Return result for n        return der[n];    }         // Driver program    public static void main (String[] args)    {        int n = 4;        System.out.println("Count of Derangements is " +                            countDer(n));         }} // This code is contributed by vt_m
# A Dynamic programming based Python3# program to count derangements def countDer(n):         # Create an array to store    # counts for subproblems    der = [0 for i in range(n + 1)]         # Base cases    der[1] = 0    der[2] = 1         # Fill der[0..n] in bottom up manner    # using above recursive formula    for i in range(3, n + 1):        der[i] = (i - 1) * (der[i - 1] +                            der[i - 2])             # Return result for n    return der[n] # Driver Coden = 4print("Count of Derangements is ", countDer(n)) # This code is contributed by Azkia Anam.
// A Dynamic programming based// C# program to count derangementsusing System; class GFG{         // Function to count    // derangements    static int countDer(int n)    {        // Create an array to store        // counts for subproblems        int []der = new int[n + 1];             // Base cases        der[1] = 0;        der[2] = 1;             // Fill der[0..n] in bottom up        // manner using above recursive        // formula        for (int i = 3; i <= n; ++i)            der[i] = (i - 1) * (der[i - 1] +                                der[i - 2]);             // Return result for n        return der[n];    }         // Driver code    public static void Main ()    {        int n = 4;        Console.Write("Count of Derangements is " +                       countDer(n));         }} // This code is contributed by nitin mittal
<?php// A Dynamic programming based PHP// program to count derangements function countDer($n){    // Create an array to store    // counts for subproblems     // Base cases    $der[1] = 0;    $der[2] = 1;     // Fill der[0..n] in bottom up manner    // using above recursive formula    for ($i = 3; $i <= $n; ++$i)        $der[$i] = ($i - 1) * ($der[$i - 1] +                               $der[$i - 2]);     // Return result for n    return $der[$n];} // Driver code$n = 4;echo "Count of Derangements is ",                    countDer($n); // This code is contributed by aj_36?>
<script>     // A Dynamic programming based    // javascript program to count    // derangements         // Function to count    // derangements    function countDer(n)    {        // Create an array to store        // counts for subproblems        let der = new Array(n + 1);              // Base cases        der[1] = 0;        der[2] = 1;              // Fill der[0..n] in bottom up        // manner using above recursive        // formula        for (let i = 3; i <= n; ++i)            der[i] = (i - 1) * (der[i - 1] +                                der[i - 2]);              // Return result for n        return der[n];    }         let n = 4;    document.write(    "Count of Derangements is " + countDer(n)    );     </script>
Count of Derangements is 9
Time Complexity : O(n) Auxiliary Space : O(n)Thanks to Utkarsh Trivedi for suggesting the above solution.
A More Efficient Solution Without using Extra Space.
As we only need to remember only two previous values So, instead of Storing the values in an array two variables can be used to just store the required previous only.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation of the above// approach #include <iostream>using namespace std; int countDer(int n){     // base case    if (n == 1 or n == 2) {        return n - 1;    }     // Variable for just storing    // previous values    int a = 0;    int b = 1;     // using above recursive formula    for (int i = 3; i <= n; ++i) {        int cur = (i - 1) * (a + b);        a = b;        b = cur;    }     // Return result for n    return b;} // Driver Codeint main(){     cout << "Count of Dearrangements is " << countDer(4);    return 0;}  // Code contributed by skagnihotri
// Java implementation of the// above approach import java.io.*; class GFG {       // Function to count derangements     static int countDer(int n) {        // Base case          if(n == 1 || n == 2) {            return n-1;        }               // Variable for storing prev values        int a = 0;          int b = 1;               // manner using above recursive formula        for (int i = 3; i <= n; ++i) {            int cur = (i-1)*(a+b);            a = b;              b = cur;        }                     // Return result for n        return b;    }           // Driver Code    public static void main (String[] args)     {        int n = 4;        System.out.println("Count of Dearrangements is " +                             countDer(n));           }     // Code contributed by skagnihotri}
# Python program to count derangements   def countDer(n):           # Base Case    if n == 1 or n == 2:      return n-1;             # Variables for storing previous values    a = 0    b = 1         # using above recursive formula    for i in range(3, n + 1):        cur = (i-1)*(a+b)        a = b        b = cur             # Return result for n    return b   # Driver Coden = 4print("Count of Dearrangements is ", countDer(n))# Code contributed by skagnihotri
// C# implementation of the above// approachusing System; class GFG{     // Function to count// derangementsstatic int countDer(int n){         // Base case    if (n == 1 || n == 2)    {        return n - 1;    }     // Variable for just storing    // previous values    int a = 0;    int b = 1;     // Using above recursive formula    for(int i = 3; i <= n; ++i)    {        int cur = (i - 1) * (a + b);        a = b;        b = cur;    }     // Return result for n    return b;} // Driver codepublic static void Main(){    Console.Write("Count of Dearrangements is " +                   countDer(4));}} // This code is contributed by koulick_sadhu
<script>     // Javascript implementation    // of the above approach         // Function to count    // derangements    function countDer(n)    {         // Base case        if (n == 1 || n == 2)        {            return n - 1;        }         // Variable for just storing        // previous values        let a = 0;        let b = 1;         // Using above recursive formula        for(let i = 3; i <= n; ++i)        {            let cur = (i - 1) * (a + b);            a = b;            b = cur;        }         // Return result for n        return b;    }         document.write("Count of Derangements is "    + countDer(4)); </script>
Count of Derangements is 9
Time Complexity: O(n) Auxiliary Space: O(1)
Thanks to Shubham Kumar for suggesting the above solution.
References: https://en.wikipedia.org/wiki/Derangement
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
nitin mittal
jit_t
skagnihotri1
koulick_sadhu
decode2207
rameshtravel07
divyesh072019
adnanirshad158
amartyaghoshgfg
prophet1999
Combinatorial
Dynamic Programming
Mathematical
Dynamic Programming
Mathematical
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Count ways to reach the nth stair using step 1, 2 or 3
Combinational Sum
Count of subsets with sum equal to X
Python program to get all subsets of given size of a set
Print all subsets of given size of a set
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Largest Sum Contiguous Subarray
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16