Count unique paths with given sum in an N | Javascript program for the above approach ; Function for counting total no of paths possible with the sum is equal to X ; If the path of the sum from the root to current node is stored in sum ; If already computed ; Count different no of paths using all possible ways ; Return total no of paths ; Driver Code ; Stores the number of ways to obtains sums 0 to X ; Function call
var mod = 1000000007 ; function findTotalPath ( X , n , dp ) { if ( X == 0 ) { return 1 ; } var ans = 0 ; if ( dp [ X ] != - 1 ) { return dp [ X ] ; } for ( var i = 1 ; i <= Math . min ( X , n ) ; ++ i ) { ans += findTotalPath ( X - i , n , dp ) % mod ; ans %= mod ; } return dp [ X ] = ans ; } var n = 3 , X = 2 ; var dp = Array ( X + 1 ) . fill ( - 1 ) ; document . write ( findTotalPath ( X , n , dp ) ) ;