Count Derangements ( Permutation such that no element appears in its original position ) | Java implementation of the above approach ; Function to count derangements ; Base case ; Variable for storing prev values ; manner using above recursive formula ; Return result for n ; Driver Code
import java . io . * ; class GFG { static int countDer ( int n ) { if ( n == 1 n == 2 ) { return n - 1 ; } int a = 0 ; int b = 1 ; for ( int i = 3 ; i <= n ; ++ i ) { int cur = ( i - 1 ) * ( a + b ) ; a = b ; b = cur ; } return b ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( " Count ▁ of ▁ Dearrangements ▁ is ▁ " + countDer ( n ) ) ; } }