Count derangements

Moderate
0/80
Average time to solve is 35m
profile
Contributed by
103 upvotes
Asked in companies
AmazonInfo Edge India (Naukri.com)Cisco

Problem statement

A Derangement is a permutation of ‘N’ elements, such that no element appears in its original position. For example, an instance of derangement of {0, 1, 2, 3} is {2, 3, 1, 0}, because 2 present at index 0 is not at its initial position which is 2 and similarly for other elements of the sequence.

Given a number ‘N’, find the total number of derangements possible of a set of 'N’ elements.

Note:
The answer could be very large, output answer %(10 ^ 9 + 7).
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line and the only line of each test case contains an integer ‘N’ denoting the number of elements whose derangements are to be counted.
Output Format:
For each test case, return the total number of derangements of a set of ‘N’ elements. 
Note:
You don't need to print anything, it has been already taken care of. Just implement the given function. 
Constraints:
1 <= T <= 100
1 <= N <= 3000

Time limit: 1 sec
Sample Input 1:
2
2
3
Sample Output 1:
1
2
Explanation of Sample Output 1:
In test case 1, For two elements say {0, 1}, there is only one possible derangement {1, 0}. 1 is present at index 0 and not at its actual position, that is, 1. Similarly, 0 is present at index 1 and not at its actual position, that is, 0.

In test case 2, For three elements say {0, 1, 2}, there are two possible derangements {2, 0, 1} and {1, 2, 0}. In both the derangements, no element is present at its actual position.

alt text

Sample Input 2:
2
1
4
Sample Output 2:
0
9
Explanation of Sample Output 2:
In test case 1, For the array = {0}, there is no possible derrangements. Hence the answer is 0 (zero).

In test case 2, For the array elements = {0, 1, 2, 3}, total 9 derrangements are possible. One of them is: { 3, 2, 1, 0}.
Hint

Think about how the position for each element will be assigned.

Approaches (3)
Recursive approach.

Let’s understand this approach with an example.

 

Consider ‘N’ = 4, the elements will be {0, 1, 2 ,3}.

  • The element 0 can be placed at any index except 0 because the element cannot be placed at the original position. So, the number of ways of placing 0 is 3, that is, at positions 1, 2, or 3.
  • To place element 1, we need to consider two situations:
        1. 0 is placed at index 2 or 3 (index other than the index of current element). In this case, the number of ways in which element 1 can be placed is 2, that is, at positions 0 or {2,3} (because 0 will already be placed at position 2 or 3).
        2. 0 is placed at position 1. In this case, 0 and 1 have swapped places and hence 1 is already deranged. So, we need not reconsider it.
  • For element 2, we again consider two conditions:
        1. None among {0,1} have already swapped places with 2, that is, it is still at the original position. In this case, only 1 position is left for 2 to be placed as 2 elements {0,1} have already taken 2 positions and it cannot be placed at the original position.
        2. One among {0,1} when deranged has swapped position with 2. So, no need to reconsider it.
  • If any other position is left for 3 other than index 3, the permutation generated is correct, else we discard it.

 

Below is the recursive relation for it.  

 

countDerangement(n) = ('N' - 1) * [countDerangement('N' - 1) + countDerangement('N' - 2)]

 

How does the above recursive relation work? 

 

An element can be placed at any index other than its original index and the indexes where previous elements are placed, that means, there are 'N' - 1 possible positions, which explains multiplication by ('N' - 1). 

 

There are two possibilities:

 

  1. The next element is still at its original position, so we send ‘n-1’ in recursion. This means now there are 'N' - 1 elements, 'N' - 1 positions and every element has 'N' - 2 choices.
  2. The next element was already swapped by any previous element, we don’t reconsider it as it is already deranged, so we pass 'N' - 2 in recursion. This means now there are 'N' - 2 elements, 'N' - 2 positions and every element has 'N' - 3 choices.

 

The steps are as follows:

 

  • Take the base condition as:
        1. If 'N' is equal to 1, return 0 because this means all the other elements have been deranged and only one element is left which could not find any other position and is still placed at its original place.
        2. If 'N' is equal to 2, return 1.
  • Recur to find all possible permutations using

            countDerangement('N')=('N' - 1) * [countDerangement('N' - 1)+countDerangement('N' - 2)].

Time Complexity

O(2 ^ N), Where 'N' is the number of elements.

 

Since all possible permutations are found such that no element appears in its original position. Thus the time complexity is O(2 ^ N).

Space Complexity

O(N), Where 'N' is the number of elements.

 

Since recursive stack uses space of the order 'N'. Thus the space complexity will be O(N).

Code Solution
(100% EXP penalty)
Count derangements
Full screen
Console