Count all unique outcomes possible by performing S flips on N coins | Dimensions of the DP table ; Stores the dp states ; Function to recursively count the number of unique outcomes possible by performing S flips on N coins ; Base Case ; If the count for the current state is not calculated , then calculate it recursively ; Otherwise return the already calculated value ; Driver Code
let size = 100 ; let ans = new Array ( size ) ; function initialize ( ) { for ( let i = 0 ; i < size ; i ++ ) { ans [ i ] = new Array ( size ) ; for ( let j = 0 ; j < size ; j ++ ) { ans [ i ] [ j ] = 0 ; } } } function numberOfUniqueOutcomes ( n , s ) { if ( s < n ) ans [ n ] [ s ] = 0 ; else if ( n == 1 n == s ) ans [ n ] [ s ] = 1 ; else if ( ans [ n ] [ s ] == 0 ) { ans [ n ] [ s ] = numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ; } return ans [ n ] [ s ] ; } initialize ( ) ; let N = 5 , S = 8 ; document . write ( numberOfUniqueOutcomes ( N , S ) ) ;