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...
Nagarro Technical Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter