Friends Pairing Problem | Javascript program for solution of friends pairing problem Using Recursion ; Returns count of ways n people can remain single or paired up . ; Driver code
let dp = new Array ( 1000 ) ; function countFriendsPairings ( n ) { if ( dp [ n ] != - 1 ) return dp [ n ] ; if ( n > 2 ) return dp [ n ] = countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ; else return dp [ n ] = n ; } for ( let i = 0 ; i < 1000 ; i ++ ) dp [ i ] = - 1 ; let n = 4 ; document . write ( countFriendsPairings ( n ) ) ;