# Number of Paths of Weight W in a K-ary tree

Given a K-ary tree, where each node is having K children and each edge has some weight. All the edges i.e. K, that goes from a particular node to all its children have weights in ascending order 1, 2, 3, …, K. Find the number of paths having total weight as W (sum of all edge weights in the path) starting from root and containing atleast one edge of weight atleast M.

**Examples:**

Input :W = 3, K = 3, M = 2Output :3Explanation :One path can be (1 + 2), second can be (2 + 1) and third is 3.Input :W = 4, K = 3, M = 2Output :6

**Approach:** This problem can be solved using dynamic programming approach. The idea is to maintain two states, one for the current weight to be required and other one for a boolean variable which denotes that the current path has included an edge of weight atleast M or not. Iterate over all possible edge weights i.e. K and recursively solve for the weight **W – i for 1 ≤ i ≤ K**. If the current edge weight is more than or equal to M, set the boolean variable as 1 for the next call.

Below is the implementation of above approach.

## C++

`// C++ program to count the number of ` `// paths with weight W in a K-ary tree ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of ways ` `// having weight as wt in K-ary tree ` `int` `solve(` `int` `dp[][2], ` `int` `wt, ` `int` `K, ` `int` `M, ` ` ` `int` `used) ` `{ ` ` ` `// Return 0 if weight becomes less ` ` ` `// than zero ` ` ` `if` `(wt < 0) ` ` ` `return` `0; ` ` ` ` ` `if` `(wt == 0) { ` ` ` ` ` `// Return one only if the ` ` ` `// current path has included ` ` ` `// edge weight of atleast M ` ` ` `if` `(used) ` ` ` `return` `1; ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `if` `(dp[wt][used] != -1) ` ` ` `return` `dp[wt][used]; ` ` ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 1; i <= K; i++) { ` ` ` ` ` `// If the current edge weight ` ` ` `// is greater than or equal to ` ` ` `// M, set used as true ` ` ` `if` `(i >= M) ` ` ` `ans += solve(dp, wt - i, ` ` ` `K, M, used | 1); ` ` ` `else` ` ` `ans += solve(dp, wt - i, ` ` ` `K, M, used); ` ` ` `} ` ` ` `return` `dp[wt][used] = ans; ` `} ` ` ` `// Driver Code to test above function ` `int` `main() ` `{ ` ` ` `int` `W = 3, K = 3, M = 2; ` ` ` `int` `dp[W + 1][2]; ` ` ` `memset` `(dp, -1, ` `sizeof` `(dp)); ` ` ` `cout << solve(dp, W, K, M, 0) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count the number of ` `# paths with weight W in a K-ary tree ` `import` `numpy as np ` ` ` `# Function to return the number of ways ` `# having weight as wt in K-ary tree ` `def` `solve(dp, wt, K, M, used) : ` ` ` ` ` `# Return 0 if weight becomes less ` ` ` `# than zero ` ` ` `if` `(wt < ` `0` `) : ` ` ` `return` `0` ` ` ` ` `if` `(wt ` `=` `=` `0` `) : ` ` ` ` ` `# Return one only if the ` ` ` `# current path has included ` ` ` `# edge weight of atleast M ` ` ` `if` `(used) : ` ` ` `return` `1` ` ` `return` `0` ` ` ` ` `if` `(dp[wt][used] !` `=` `-` `1` `) : ` ` ` `return` `dp[wt][used] ` ` ` ` ` `ans ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, K ` `+` `1` `) : ` ` ` ` ` `# If the current edge weight ` ` ` `# is greater than or equal to ` ` ` `# M, set used as true ` ` ` `if` `(i >` `=` `M) : ` ` ` `ans ` `+` `=` `solve(dp, wt ` `-` `i, ` ` ` `K, M, used | ` `1` `) ` ` ` `else` `: ` ` ` `ans ` `+` `=` `solve(dp, wt ` `-` `i, ` ` ` `K, M, used) ` ` ` ` ` `dp[wt][used] ` `=` `ans ` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `W ` `=` `3` ` ` `K ` `=` `3` ` ` `M ` `=` `2` ` ` `dp ` `=` `np.ones((W ` `+` `1` `, ` `2` `)); ` ` ` `dp ` `=` `-` `1` `*` `dp ` ` ` `print` `(solve(dp, W, K, M, ` `0` `)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the number of ` `// paths with weight W in a K-ary tree ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number of ways ` ` ` `// having weight as wt in K-ary tree ` ` ` `public` `static` `int` `solve(` `int` `[,] dp, ` `int` `wt, ` `int` `K, ` `int` `M, ` `int` `used) ` ` ` `{ ` ` ` `// Return 0 if weight becomes less ` ` ` `// than zero ` ` ` `if` `(wt < 0) ` ` ` `return` `0; ` ` ` ` ` `if` `(wt == 0) { ` ` ` ` ` `// Return one only if the ` ` ` `// current path has included ` ` ` `// edge weight of atleast M ` ` ` `if` `(used == 1) ` ` ` `return` `1; ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `if` `(dp[wt,used] != -1) ` ` ` `return` `dp[wt,used]; ` ` ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 1; i <= K; i++) { ` ` ` ` ` `// If the current edge weight ` ` ` `// is greater than or equal to ` ` ` `// M, set used as true ` ` ` `if` `(i >= M) ` ` ` `ans += solve(dp, wt - i, ` ` ` `K, M, used | 1); ` ` ` `else` ` ` `ans += solve(dp, wt - i, ` ` ` `K, M, used); ` ` ` `} ` ` ` `return` `dp[wt,used] = ans; ` ` ` `} ` ` ` ` ` `// Driver Code to test above function ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `W = 3, K = 3, M = 2; ` ` ` `int` `[,] dp = ` `new` `int` `[W + 1,2]; ` ` ` `for` `(` `int` `i = 0;i < W + 1; i++) ` ` ` `for` `(` `int` `j = 0; j < 2; j++) ` ` ` `dp[i,j] = -1; ` ` ` `Console.Write(solve(dp, W, K, M, 0) + ` `"\n"` `); ` ` ` `} ` ` ` `//This code is contributed by DrRoot_ ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(W * K)

## Recommended Posts:

- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Number of unique paths in tree such that every path has a value greater than K
- Find the weight of the minimum spanning tree
- Choose maximum weight with given weight and value ratio
- Print all k-sum paths in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Given a binary tree, print all root-to-leaf paths
- Root to leaf paths having equal lengths in a Binary Tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Puzzle | Minimum number steps to weigh 1 kg rice with 1gm weight
- Number of paths with exactly k coins
- Number of palindromic paths in a matrix
- Count number of paths with at-most k turns
- Number of different cyclic paths of length N in a tetrahedron
- Maths behind number of paths in matrix problem

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.