# Find the maximum GCD of the siblings of a Binary Tree

Given a 2d-array **arr[][]** which represents the nodes of a Binary tree, the task is to find the maximum GCD of the siblings of this tree without actually constructing it.

**Example:**

Input:arr[][] = {{4, 5}, {4, 2}, {2, 3}, {2, 1}, {3, 6}, {3, 12}}

Output:6

Explanation:

For the above tree, the maximum GCD for the sibilings is formed for the nodes 6 and 12 for the children of node 3.

Input:arr[][] = {{5, 4}, {5, 8}, {4, 6}, {4, 9}, {8, 10}, {10, 20}, {10, 30}}

Output:10

**Approach:** The idea is to form a vector and store the tree in the form of the vector. After storing the tree in the form of a vector, the following cases occur:

- If the vector size is
**0 or 1**, then print 0 as GCD could not be found. - For all other cases, since we store the tree in the form of a pair, we consider the first values of two pairs and compare them.

**For example,**let’s assume there are two pairs in the vector A and B. We check if:A.first == B.first

If both of them match, then both of them belongs to the same parent. Therefore, we compute the GCD of the second values in the pairs and finally print the maximum of all such GCD’s.

Below is the implementation of the above approach:

## CPP

`// C++ program to find the maximum ` `// GCD of the siblings of a binary tree ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find maximum GCD ` `int` `max_gcd(vector<pair<` `int` `, ` `int` `> >& v) ` `{ ` ` ` `// No child or Single child ` ` ` `if` `(v.size() == 1 || v.size() == 0) ` ` ` `return` `0; ` ` ` ` ` `// To get the first pair ` ` ` `pair<` `int` `, ` `int` `> a = v[0]; ` ` ` `pair<` `int` `, ` `int` `> b; ` ` ` `int` `ans = INT_MIN; ` ` ` `for` `(` `int` `i = 1; i < v.size(); i++) { ` ` ` `b = v[i]; ` ` ` ` ` `// If both the pairs belongs to ` ` ` `// the same parent ` ` ` `if` `(b.first == a.first) ` ` ` ` ` `// Update ans with the max ` ` ` `// of current gcd and ` ` ` `// gcd of both children ` ` ` `ans ` ` ` `= max(ans, ` ` ` `__gcd(a.second, ` ` ` `b.second)); ` ` ` ` ` `// Update previous ` ` ` `// for next iteration ` ` ` `a = b; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `vector<pair<` `int` `, ` `int` `> > v; ` ` ` `v.push_back(make_pair(5, 4)); ` ` ` `v.push_back(make_pair(5, 8)); ` ` ` `v.push_back(make_pair(4, 6)); ` ` ` `v.push_back(make_pair(4, 9)); ` ` ` `v.push_back(make_pair(8, 10)); ` ` ` `v.push_back(make_pair(10, 20)); ` ` ` `v.push_back(make_pair(10, 30)); ` ` ` ` ` `cout << max_gcd(v); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## python3

`# Python3 program to find the maximum ` `# GCD of the siblings of a binary tree ` `from` `math ` `import` `gcd ` ` ` `# Function to find maximum GCD ` `def` `max_gcd(v): ` ` ` ` ` `# No child or Single child ` ` ` `if` `(` `len` `(v) ` `=` `=` `1` `or` `len` `(v) ` `=` `=` `0` `): ` ` ` `return` `0` ` ` ` ` `# To get the first pair ` ` ` `a ` `=` `v[` `0` `] ` ` ` `ans ` `=` `-` `10` `*` `*` `9` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(v)): ` ` ` `b ` `=` `v[i] ` ` ` ` ` `# If both the pairs belongs to ` ` ` `# the same parent ` ` ` `if` `(b[` `0` `] ` `=` `=` `a[` `0` `]): ` ` ` ` ` `# Update ans with the max ` ` ` `# of current gcd and ` ` ` `# gcd of both children ` ` ` `ans ` `=` `max` `(ans, gcd(a[` `1` `], b[` `1` `])) ` ` ` ` ` `# Update previous ` ` ` `# for next iteration ` ` ` `a ` `=` `b ` ` ` `return` `ans ` ` ` `# Driver function ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `v` `=` `[] ` ` ` `v.append([` `5` `, ` `4` `]) ` ` ` `v.append([` `5` `, ` `8` `]) ` ` ` `v.append([` `4` `, ` `6` `]) ` ` ` `v.append([` `4` `, ` `9` `]) ` ` ` `v.append([` `8` `, ` `10` `]) ` ` ` `v.append([` `10` `, ` `20` `]) ` ` ` `v.append([` `10` `, ` `30` `]) ` ` ` ` ` `print` `(max_gcd(v)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

10

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check if two nodes in a Binary Tree are siblings
- Find maximum level sum in Binary Tree
- Find maximum (or minimum) in Binary Tree
- Find maximum among all right nodes in Binary Tree
- Find maximum vertical sum in binary tree
- Find the maximum path sum between two leaves of a binary tree
- Find maximum level product in Binary Tree
- Find the maximum node at a given level in a binary tree
- Find Maximum Level Sum in Binary Tree using Recursion
- Find the node with maximum value in a Binary Search Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Find the node with maximum value in a Binary Search Tree using recursion
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Number of siblings of a given Node in n-ary Tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Maximum XOR path of a Binary Tree
- Maximum spiral sum in Binary Tree
- Maximum width of a binary tree
- Maximum Path Sum in a Binary 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.