Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Driver code
function numberOfWays ( x ) { let dp = Array ( x + 1 ) . fill ( 0 ) ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; } let x = 3 ; document . write ( numberOfWays ( x ) ) ;