Count unique paths with given sum in an N | C # 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
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int findTotalPath ( int X , int n , int [ ] dp ) { if ( X == 0 ) { return 1 ; } int ans = 0 ; if ( dp [ X ] != - 1 ) { return dp [ X ] ; } for ( int i = 1 ; i <= Math . Min ( X , n ) ; ++ i ) { ans += findTotalPath ( X - i , n , dp ) % mod ; ans %= mod ; } dp [ X ] = ans ; return ans ; } public static void Main ( ) { int n = 3 , X = 2 ; int [ ] dp = new int [ X + 1 ] ; Array . Fill ( dp , - 1 ) ; Console . WriteLine ( findTotalPath ( X , n , dp ) ) ; } }