Given a n-ary tree having nodes with a particular weight, our task is to find out the maximum sum of the minimum difference of divisors of each node from root to leaf.
18 / \ 7 15 / \ \ 4 12 2 / 9
The maximum sum is along the path 18 -> 7 -> 12 -> 9
Minimum difference between divisors of 18 = 6 – 3 = 3
Minimum difference between divisors of 7 = 7 – 1 = 6
Minimum difference between divisors of 12 = 4 – 3 = 1
Minimum difference between divisors of 9 = 3 – 3 = 0
20 / \ 13 14 / \ \ 10 8 26 / 25
The maximum sum is along the path 20 -> 14 -> 26
To solve the problem mentioned above we can store the minimum difference between the divisors at each node in an array using the depth first traversal. Now, the task is to find out the minimum difference between the divisors.
- We can observe that for any number N, the smallest divisor x in the range [√N, N] will have the least difference between x and N/x.
- At each node, calculate the minimum difference between divisors, and finally start filling the array using the results of the children and calculate the maximum sum possible.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Find sum of divisors of all the divisors of a natural number
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Maximize count of nodes disconnected from all other nodes in a Graph
- Divisors of n-square that are not divisors of n
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count of all prime weight nodes between given nodes in the given Tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Count of nodes having odd divisors in the given subtree for Q queries
- Break a number such that sum of maximum divisors of all parts is minimum
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Difference between sum of even and odd valued nodes in a Binary Tree
- Sum of all odd nodes in the path connecting two given nodes
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Maximize the sum of products of the degrees between any two vertices of the tree
- Sub-tree with minimum color difference in a 2-coloured tree
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.
Improved By : princi singh