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.
Input : W = 3, K = 3, M = 2 Output : 3 Explanation : One path can be (1 + 2), second can be (2 + 1) and third is 3. Input : W = 4, K = 3, M = 2 Output : 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.
Time Complexity: O(W * K)
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Choose maximum weight with given weight and value ratio
- Count nodes in the given tree whose weight is a fibonacci number
- Count the nodes in the given tree whose weight is a powerful number
- Count the nodes in the given Tree whose weight is a Perfect Number
- Find the weight of the minimum spanning tree
- Count the nodes in the given tree whose weight is prime
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is a power of two
- Count the nodes in the given tree whose weight is even parity
- Count the nodes in the given tree whose sum of digits of weight is odd
- Count the nodes of the given tree whose weight has X as a factor
- Product of minimum edge weight between all pairs of a Tree
- Count of Nodes which has Prime Digit sum weight in a Tree
- Count of all prime weight nodes between given nodes in the given Tree
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Count number of times each Edge appears in all possible paths of a given Tree
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Number of unique paths in tree such that every path has a value greater than K
- Puzzle | Minimum number steps to weigh 1 kg rice with 1gm weight
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.