Number of ways to get a given sum with n number of m | Java function to calculate the number of ways to achieve sum x in n no of throwsVal ; Function to calculate recursively the number of ways to get sum in given throwsVal and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throwsVal - 1 no of throwsVal left ; Inserting present values in dp ; Driver code
class GFG { static int mod = 1000000007 ; static int [ ] [ ] dp = new int [ 55 ] [ 55 ] ; static int NoofWays ( int face , int throwsVal , int sum ) { if ( sum == 0 && throwsVal == 0 ) { return 1 ; } if ( sum < 0 throwsVal == 0 ) { return 0 ; } if ( dp [ throwsVal ] [ sum ] != - 1 ) { return dp [ throwsVal ] [ sum ] ; } int ans = 0 ; for ( int i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throwsVal - 1 , sum - i ) ; } return dp [ throwsVal ] [ sum ] = ans ; } public static void main ( String [ ] args ) { int faces = 6 , throwsVal = 3 , sum = 12 ; for ( int i = 0 ; i < 55 ; i ++ ) { for ( int j = 0 ; j < 55 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( NoofWays ( faces , throwsVal , sum ) ) ; } }