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.

AnswerBot
10d

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

Help your peers!
Add answer anonymously...
Nagarro Technology 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