Count Derangements | 4 Approaches | Dynamic Programming | Nagarro Summer internship

preview_player
Показать описание


You are given N balls numbered from 1 to N and there are N baskets numbered from 1 to N in front of you, the ith basket is meant for the ith ball. Calculate the number of ways in which no ball goes into its respective basket.

Input: 3
Output: 2
Explaination: The (number-basket) pairs are
[(1-3),(2-1),(3-2)] and [(1-2),(2-3),(3-2)].

Рекомендации по теме
Комментарии
Автор

Case 2 : better explanation : generate derangements for 1 -> n and then for each derrangement we can replace 0th element with any of the n-1 elements. Note: after derrangement the character at ith position will not be in it's own position hence this replacement will be different from the replacement we made in Case 1 where we swap with the ith element before generating derrangements for the other n-2 elements.

ArpanSen-znkv