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.

**Examples:**

Input:18 / \ 7 15 / \ \ 4 12 2 / 9

Output:10

Explanation:

The maximum sum is along the path18 -> 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

Input:

20 / \ 13 14 / \ \ 10 8 26 / 25

Output:17

Explanation:

The maximum sum is along the path20 -> 14 -> 26

**Approach:**

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 above approach:

## C++

`// C++ program to maximize the sum of ` `// minimum difference of divisors ` `// of nodes in an n-ary tree ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Array to store the ` `// result at each node ` `int` `sub[100005]; ` ` ` `// Function to get minimum difference ` `// between the divisors of a number ` `int` `minDivisorDifference(` `int` `n) ` `{ ` ` ` `int` `num1; ` ` ` `int` `num2; ` ` ` ` ` `// Iterate from square ` ` ` `// root of N to N ` ` ` `for` `(` `int` `i = ` `sqrt` `(n); i <= n; i++) { ` ` ` `if` `(n % i == 0) { ` ` ` `num1 = i; ` ` ` `num2 = n / i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `// return absolute difference ` ` ` `return` `abs` `(num1 - num2); ` `} ` ` ` `// DFS function to calculate the maximum sum ` `int` `dfs(vector<` `int` `> g[], ` `int` `u, ` `int` `par) ` `{ ` ` ` ` ` `// Store the min difference ` ` ` `sub[u] = minDivisorDifference(u); ` ` ` ` ` `int` `mx = 0; ` ` ` `for` `(` `auto` `c : g[u]) { ` ` ` `if` `(c != par) { ` ` ` `int` `ans = dfs(g, c, u); ` ` ` `mx = max(mx, ans); ` ` ` `} ` ` ` `} ` ` ` `// Add the maximum of ` ` ` `// all children to sub[u] ` ` ` `sub[u] += mx; ` ` ` ` ` `// Return maximum sum of ` ` ` `// node 'u' to its parent ` ` ` `return` `sub[u]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `vector<` `int` `> g[100005]; ` ` ` ` ` `int` `edges = 6; ` ` ` ` ` `g[18].push_back(7); ` ` ` `g[7].push_back(18); ` ` ` ` ` `g[18].push_back(15); ` ` ` `g[15].push_back(18); ` ` ` ` ` `g[15].push_back(2); ` ` ` `g[2].push_back(15); ` ` ` ` ` `g[7].push_back(4); ` ` ` `g[4].push_back(7); ` ` ` ` ` `g[7].push_back(12); ` ` ` `g[12].push_back(7); ` ` ` ` ` `g[12].push_back(9); ` ` ` `g[9].push_back(12); ` ` ` ` ` `int` `root = 18; ` ` ` ` ` `cout << dfs(g, root, -1); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

10

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.

## Recommended Posts:

- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Minimum no. of iterations to pass information to all nodes in the tree
- Minimum distance to visit all the nodes of an undirected weighted tree
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Minimum time required to visit all the special nodes of a Tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Difference between sum of even and odd valued nodes in a Binary Tree
- Sub-tree with minimum color difference in a 2-coloured tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Count of nodes having odd divisors in the given subtree for Q queries
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Difference between sums of odd level and even level nodes of a Binary Tree
- Count of all prime weight nodes between given nodes in the given Tree
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Minimum number of Square Free Divisors
- Maximize the sum of products of the degrees between any two vertices of the 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 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.