# Count number of paths whose weight is exactly X and has at-least one edge of weight M

Given an infinite tree and three numbers N, M, and X which has exactly N child from every node. Every edge has a weight of 1, 2, 3, 4..N. The task is to find the count of paths whose weight is exactly X and has a minimum of one edge of weight M in it.

The diagram above shows a tree shown till level-3 and N = 3.

**Examples:**

Input:N = 3, M = 2, X = 3Output:2 The path 1-2 and 2-1 in the image aboveInput:N = 2, M = 1, X = 4Output:4

**Approach:** The problem can be solved using Dynamic Programming and memoization. We will use a top-down approach to solve this problem. Recur starting from the root with sum initially as X, and recursively traverse all paths possible( which is from 1 to N). If the node is equal to M, then the second parameter becomes true, else it stays the same which has been passed in the previous call. Store the value in a DP[][] table to avoid visiting same states twice.

Below is the implementation of the above approach.

## C++

`// C++ program to count the number of paths ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define max 4 ` `#define c 2 ` ` ` `// Function to find the number of paths ` `int` `countPaths(` `int` `sum, ` `int` `get, ` `int` `m, ` `int` `n, ` `int` `dp[]) ` `{ ` ` ` ` ` `// If the summation is more than X ` ` ` `if` `(sum < 0) ` ` ` `return` `0; ` ` ` ` ` `// If exactly X weights have reached ` ` ` `if` `(sum == 0) ` ` ` `return` `get; ` ` ` ` ` `// Already visited ` ` ` `if` `(dp[sum][get] != -1) ` ` ` `return` `dp[sum][get]; ` ` ` ` ` `// Count paths ` ` ` `int` `res = 0; ` ` ` ` ` `// Traverse in all paths ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// If the edge weight is M ` ` ` `if` `(i == m) ` ` ` `res += countPaths(sum - i, 1, m, n, dp); ` ` ` `else` `// Edge's weight is not M ` ` ` `res += countPaths(sum - i, get, m, n, dp); ` ` ` `} ` ` ` ` ` `dp[sum][get] = res; ` ` ` ` ` `return` `dp[sum][get]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 3, m = 2, x = 3; ` ` ` ` ` `int` `dp[max + 1]; ` ` ` ` ` `// Initialized the DP array with -1 ` ` ` `for` `(` `int` `i = 0; i <= max; i++) ` ` ` `for` `(` `int` `j = 0; j < 2; j++) ` ` ` `dp[i][j] = -1; ` ` ` ` ` `// Function to count paths ` ` ` `cout << countPaths(x, 0, m, n, dp); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the number of paths ` ` ` `public` `class` `GFG{ ` ` ` ` ` `static` `int` `max = ` `4` `; ` ` ` `static` `int` `c = ` `2` `; ` ` ` ` ` `// Function to find the number of paths ` ` ` `static` `int` `countPaths(` `int` `sum, ` `int` `get, ` `int` `m, ` `int` `n, ` `int` `dp[][]) ` ` ` `{ ` ` ` ` ` `// If the summation is more than X ` ` ` `if` `(sum < ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// If exactly X weights have reached ` ` ` `if` `(sum == ` `0` `) ` ` ` `return` `get; ` ` ` ` ` `// Already visited ` ` ` `if` `(dp[sum][get] != -` `1` `) ` ` ` `return` `dp[sum][get]; ` ` ` ` ` `// Count paths ` ` ` `int` `res = ` `0` `; ` ` ` ` ` `// Traverse in all paths ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) { ` ` ` ` ` `// If the edge weight is M ` ` ` `if` `(i == m) ` ` ` `res += countPaths(sum - i, ` `1` `, m, n, dp); ` ` ` `else` `// Edge's weight is not M ` ` ` `res += countPaths(sum - i, get, m, n, dp); ` ` ` `} ` ` ` ` ` `dp[sum][get] = res; ` ` ` ` ` `return` `dp[sum][get]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `int` `n = ` `3` `, m = ` `2` `, x = ` `3` `; ` ` ` ` ` `int` `dp[][] = ` `new` `int` `[max + ` `1` `][` `2` `]; ` ` ` ` ` `// Initialized the DP array with -1 ` ` ` `for` `(` `int` `i = ` `0` `; i <= max; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `2` `; j++) ` ` ` `dp[i][j] = -` `1` `; ` ` ` ` ` `// Function to count paths ` ` ` `System.out.println(countPaths(x, ` `0` `, m, n, dp)); ` ` ` `} ` ` ` `// This code is contributed by Ryuga ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the number of paths ` `Max` `=` `4` `c ` `=` `2` ` ` `# Function to find the number of paths ` `def` `countPaths(` `Sum` `, get, m, n, dp): ` ` ` ` ` `# If the Summation is more than X ` ` ` `if` `(` `Sum` `< ` `0` `): ` ` ` `return` `0` ` ` ` ` `# If exactly X weights have reached ` ` ` `if` `(` `Sum` `=` `=` `0` `): ` ` ` `return` `get ` ` ` ` ` `# Already visited ` ` ` `if` `(dp[` `Sum` `][get] !` `=` `-` `1` `): ` ` ` `return` `dp[` `Sum` `][get] ` ` ` ` ` `# Count paths ` ` ` `res ` `=` `0` ` ` ` ` `# Traverse in all paths ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` ` ` `# If the edge weight is M ` ` ` `if` `(i ` `=` `=` `m): ` ` ` `res ` `+` `=` `countPaths(` `Sum` `-` `i, ` `1` `, m, n, dp) ` ` ` `else` `: ` `# Edge's weight is not M ` ` ` `res ` `+` `=` `countPaths(` `Sum` `-` `i, get, m, n, dp) ` ` ` ` ` `dp[` `Sum` `][get] ` `=` `res ` ` ` ` ` `return` `dp[` `Sum` `][get] ` ` ` `# Driver Code ` `n ` `=` `3` `m ` `=` `2` `x ` `=` `3` `dp ` `=` `[[` `-` `1` `for` `i ` `in` `range` `(` `2` `)] ` ` ` `for` `i ` `in` `range` `(` `Max` `+` `1` `)] ` ` ` `# Initialized the DP array with -1 ` `for` `i ` `in` `range` `(` `Max` `+` `1` `): ` ` ` `for` `j ` `in` `range` `(` `2` `): ` ` ` `dp[i][j] ` `=` `-` `1` ` ` `# Function to count paths ` `print` `(countPaths(x, ` `0` `, m, n, dp)) ` ` ` `# This code is contributed by Mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

// C# program to count the number of paths

using System;

class GFG

{

static int max = 4 ;

static int c = 2 ;

// Function to find the number of paths

static int countPaths(int sum, int get, int m,

int n, int[, ] dp)

{

// If the summation is more than X

if (sum < 0)
return 0;
// If exactly X weights have reached
if (sum == 0)
return get;
// Already visited
if (dp[sum, get] != -1)
return dp[sum, get];
// Count paths
int res = 0;
// Traverse in all paths
for (int i = 1; i <= n; i++)
{
// If the edge weight is M
if (i == m)
res += countPaths(sum - i, 1, m, n, dp);
else // Edge's weight is not M
res += countPaths(sum - i, get, m, n, dp);
}
dp[sum, get] = res;
return dp[sum, get];
}
// Driver Code
public static void Main()
{
int n = 3, m = 2, x = 3;
int[,] dp = new int[max + 1, 2];
// Initialized the DP array with -1
for (int i = 0; i <= max; i++)
for (int j = 0; j < 2; j++)
dp[i, j] = -1;
// Function to count paths
Console.WriteLine(countPaths(x, 0, m, n, dp));
}
}
// This code is contributed by Akanksha Rai
[tabbyending]

**Output:**

2

## Recommended Posts:

- Number of Paths of Weight W in a K-ary tree
- Choose maximum weight with given weight and value ratio
- Minimum cost to fill given weight in a bag
- Maximum weight transformation of a given string
- Maximum weight path ending at any element of last row in a matrix
- Count number of paths with at-most k turns
- Count all possible paths from top left to bottom right of a mXn matrix
- Number of paths with exactly k coins
- Number of palindromic paths in a matrix
- Total number of decreasing paths in a matrix
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Number of different cyclic paths of length N in a tetrahedron
- Number of unique paths in tree such that every path has a value greater than K
- Number of paths from source to destination in a directed acyclic graph
- Maximum edge removal from tree to make even forest

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.