Counting Derangements Problem
A derangement is a permutation of 'N' elements, where no element appears in its original position. For instance, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0} because each element is not in its original position.
Problem Statement:
Given a number 'N', find the total number of derangements possible for a set of 'N' elements.
Input:
The first line of input contains an integer 'T', representing the number of test cases.
The subsequent lines contain an integer 'N' for each test case, representing the number of elements for which derangements need to be counted.
Output:
For each test case, return the total number of derangements of a set of 'N' elements.
Example:
Input:
T = 1
N = 3
Output:
2
The possible derangements are {1, 2, 0} and {2, 0, 1}.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 3000
Note: The result could be very large, return the answer modulo (10^9 + 7).
Be the first one to answer
Add answer anonymously...
Top Nagarro Technical Trainee interview questions & answers
Popular interview questions of Technical Trainee
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app