![]() |
VOOZH | about |
Given two numbers, n >= 0 and 0 <= k <= n, count the number of derangements with k fixed points.
Examples:
Input : n = 3, k = 0
Output : 2
Since k = 0, no point needs to be on its
original position. So derangements
are {3, 1, 2} and {2, 3, 1}
Input : n = 3, k = 1
Output : 3
Since k = 1, one point needs to be on its
original position. So partial derangements
are {1, 3, 2}, {3, 2, 1} and {2, 1, 3}
Input : n = 7, k = 2
Output : 924
In combinatorial mathematics, the rencontres number< or D(n, k) represents count of partial derangements.
The recurrence relation to find Rencontres Number Dn, k:
D(0, 0) = 1
D(0, 1) = 0
D(n+2, 0) = (n+1) * (D(n+1, 0) + D(n, 0))
D(n, k) = nCk * D(n-k, 0))
Given the two positive integer n and k. The task is find rencontres number D(n, k) for giver n and k.
Below is Recursive solution of this approach:
924
Time Complexity: O(n * m), where n and m represents the given integers.
Auxiliary Space: O(n*m), due to recursive stack space.
Below is the implementation using Dynamic Programming:
924
Time Complexity: O(n * m), where n and m represents the given integers.
Auxiliary Space: O(n * m), where n and m represents the given integers.