Count number of paths whose weight is exactly X and has at | Javascript program to count the number of paths ; Function to find the number of paths ; If the summation is more than X ; If exactly X weights have reached ; Already visited ; Count paths ; Traverse in all paths ; If the edge weight is M ; Edge 's weight is not M ; Driver Code ; Initialized the DP array with - 1 ; Function to count paths
let max = 4 ; let c = 2 ; function countPaths ( sum , get , m , n , dp ) { if ( sum < 0 ) return 0 ; if ( sum == 0 ) return get ; if ( dp [ sum ] [ get ] != - 1 ) return dp [ sum ] [ get ] ; let res = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( i == m ) res += countPaths ( sum - i , 1 , m , n , dp ) ; else res += countPaths ( sum - i , get , m , n , dp ) ; } dp [ sum ] [ get ] = res ; return dp [ sum ] [ get ] ; } let n = 3 , m = 2 , x = 3 ; let dp = new Array ( max + 1 ) ; for ( let i = 0 ; i <= max ; i ++ ) { dp [ i ] = new Array ( 2 ) for ( let j = 0 ; j < 2 ; j ++ ) dp [ i ] [ j ] = - 1 ; } document . write ( countPaths ( x , 0 , m , n , dp ) ) ;