Count all unique outcomes possible by performing S flips on N coins | Java program for the above approach ; 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
import java . util . * ; class GFG { static int size = 100 ; static int [ ] [ ] ans = new int [ size ] [ size ] ; static void initialize ( ) { for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size ; j ++ ) { ans [ i ] [ j ] = 0 ; } } } static int numberOfUniqueOutcomes ( int n , int 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 ] ; } public static void main ( String args [ ] ) { initialize ( ) ; int N = 5 , S = 8 ; System . out . println ( numberOfUniqueOutcomes ( N , S ) ) ; } }