frodo
frodo

Reputation: 1571

Counting Derangements of an array of numbers(recursive)

I came across this problem among interview questions.

Given an array of numbers, we have to calculate the total number of derangements that are possible for this array. Derangements of an array are those permuations where no element is in the original place. There is no restriction on the kind of numbers in the array. There could also be dupliates.

I know a solution using the inclusion-exclusion principle. I was looking for a recursive formulation, using DP. This approach probably uses memoization and bitmasks. Thanks.

Upvotes: 2

Views: 919

Answers (1)

VSOverFlow
VSOverFlow

Reputation: 1035

Recurrence from Wikipedia

Derangement from Wikipedia

Where !n is the number of derangements, assuming no duplicates though.

Upvotes: 2

Related Questions