Count Derangements Problem Statement
You are tasked with finding the total number of derangements for a given set of elements. Specifically, for an integer N
, determine how many ways there are to permute a set of N
elements such that no element appears in its original position.
Definition:
A derangement is a permutation of elements where no element appears in its original position. For instance, for the set {0, 1, 2, 3}, a derangement is {2, 3, 1, 0}.
Example:
When N = 2
, the possible derangements are {1, 0} and the output will therefore be 1.
Input:
The first line contains an integer 'T' representing the number of test cases.
Each test case consists of a single integer 'N', indicating the size of the set for which derangements are to be counted.
Output:
For each test case, output the total number of derangements possible for a set with size 'N'. Each result should be printed on a new line.
Explanation:
The solution could result in a large number, thus the output should be calculated as answer % (10^9 + 7)
.
Constraints:
1 <= T <= 5
1 <= N <= 5000
- Time Limit: 1 sec
Note:
You are only required to implement the function. Printing has already been handled.
The task is to find the total number of derangements for a given set of elements, where no element appears in its original position.
Implement a function to calculate the number of derangements for a g...read more
Top Nagarro Technology Trainee interview questions & answers
Popular interview questions of Technology Trainee
Reviews
Interviews
Salaries
Users/Month