Count Derangements ( Permutation such that no element appears in its original position ) | A Naive Recursive java program to count derangements ; Function to count derangements ; Base cases ; countDer ( n ) = ( n - 1 ) [ countDer ( n - 1 ) + der ( n - 2 ) ] ; Driver Code
import java . io . * ; class GFG { static int countDer ( int n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; return ( n - 1 ) * ( countDer ( n - 1 ) + countDer ( n - 2 ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( " Count ▁ of ▁ Derangements ▁ is ▁ " + countDer ( n ) ) ; } }