Given an array arr[] of size N. The task is to find whether it is possible to make Binary Search Tree with the given array of elements such that greatest common divisor of any two vertices connected by a common edge is > 1. If possible then print Yes else print No.
Examples:
Input: arr[] = {3, 6, 9, 18, 36, 108}
Output: Yes
This is one of the possible Binary Search Tree with given array.Input: arr[] = {2, 17}
Output: No
Approach: Let DP(l, r, root) be a DP determining whether it’s possible to assemble a tree rooted at root from the sub-segment [l..r].
It’s easy to see that calculating it requires extracting such rootleft from [l..root – 1] and rootright from [root + 1..right] such that:
- gcd(aroot, arootleft) > 1
- gcd(aroot, arootright) > 1
- DP(l, root-1, rootleft) = 1
- DP(root+1, r, rootright) = 1
This can be done in O(r – l) provided we are given all DP(x, y, z) values for all sub-segments of [l..r]. Considering a total of O(n3) DP states, the final complexity is O(n4) and that’s too much.
Let’s turn our DP into DPnew(l, r, state) where the state can be either 0 or 1. It immediately turns out that DP(l, r, root) is inherited from DPnew(l, root-1, 1) and DPnew(root+1, r, 0). Now we have O(n2) states, but at the same time, all transitions are performed in linear time. Thus final complexity is O(n3) which is sufficient to pass.
Below is the implementation of the above approach:
// C++ implementation of the appraoch #include <bits/stdc++.h> using namespace std; // Maxium number of vertices #define N 705 // To store is it possible at // particular pace or not int dp[N][N][2]; // Return 1 if from l to r, it is possible with // the given state int possibleWithState(int l, int r, int state, int a[]) { // Base condition if (l > r) return 1; // If it is already calculated if (dp[l][r][state] != -1) return dp[l][r][state]; // Choose the root int root; if (state == 1) root = a[r + 1]; else root = a[l - 1]; // Traverse in range l to r for (int i = l; i <= r; i++) { // If gcd is greater than one // check for both sides if (__gcd(a[i], root) > 1) { int x = possibleWithState(l, i - 1, 1, a); if (x != 1) continue; int y = possibleWithState(i + 1, r, 0, a); if (x == 1 && y == 1) return dp[l][r][state] = 1; } } // If not possible return dp[l][r][state] = 0; } // Function that return true if it is possible // to make Binary Search Tree bool isPossible(int a[], int n) { memset(dp, -1, sizeof dp); // Sort the given array sort(a, a + n); // Check it is possible rooted at i for (int i = 0; i < n; i++) // Check at both sides if (possibleWithState(0, i - 1, 1, a) && possibleWithState(i + 1, n - 1, 0, a)) { return true; } return false; } // Driver code int main() { int a[] = { 3, 6, 9, 18, 36, 108 }; int n = sizeof(a) / sizeof(a[0]); if (isPossible(a, n)) cout << "Yes"; else cout << "No"; return 0; } |
Yes
Recommended Posts:
- Binary Search Tree | Set 1 (Search and Insertion)
- Binary Tree to Binary Search Tree Conversion
- Binary Tree to Binary Search Tree Conversion using STL set
- Binary Search Tree | Set 2 (Delete)
- Optimal Binary Search Tree | DP-24
- Floor in Binary Search Tree (BST)
- Construct a Binary Search Tree from given postorder
- How to handle duplicates in Binary Search Tree?
- Print all even nodes of Binary Search Tree
- Iterative searching in Binary Search Tree
- Inorder Successor in Binary Search Tree
- Threaded Binary Search Tree | Deletion
- Print all odd nodes of Binary Search Tree
- Insert a node in Binary Search Tree Iteratively
- Find the closest element in Binary Search 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.




