Tree of Coprimes - Problem

There is a tree (i.e., a connected, undirected graph that has no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. Each node has a value associated with it, and the root of the tree is node 0.

To represent this tree, you are given an integer array nums and a 2D array edges. Each nums[i] represents the ith node's value, and each edges[j] = [uj, vj] represents an edge between nodes uj and vj in the tree.

Two values x and y are coprime if gcd(x, y) == 1 where gcd(x, y) is the greatest common divisor of x and y.

An ancestor of a node i is any other node on the shortest path from node i to the root. A node is not considered an ancestor of itself.

Return an array ans of size n, where ans[i] is the closest ancestor to node i such that nums[i] and nums[ans[i]] are coprime, or -1 if there is no such ancestor.

Input & Output

Example 1 — Basic Tree
$ Input: nums = [2,3,3,2], edges = [[0,1],[1,2],[1,3]]
Output: [-1,0,0,1]
💡 Note: Node 0 is root (no ancestors). Node 1: gcd(3,2)=1, so ancestor is 0. Node 2: gcd(3,3)=3≠1 but gcd(3,2)=1, so ancestor is 0. Node 3: gcd(2,3)=1, so ancestor is 1.
Example 2 — Single Node
$ Input: nums = [5], edges = []
Output: [-1]
💡 Note: Only one node, which is the root, so no ancestors exist.
Example 3 — Linear Chain
$ Input: nums = [5,6,10,2,3,6,15], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
Output: [-1,0,0,0,0,0,0]
💡 Note: Node 0 is root. Node 1: gcd(6,5)=1, ancestor is 0. Node 2: gcd(10,5)=5≠1, no coprime ancestor. Node 3: gcd(2,5)=1, ancestor is 0. Node 4: gcd(3,5)=1, ancestor is 0. Node 5: gcd(6,5)=1, ancestor is 0. Node 6: gcd(15,5)=5≠1, no coprime ancestor.

Constraints

  • nums.length == n
  • 1 ≤ n ≤ 105
  • 1 ≤ nums[i] ≤ 50
  • edges.length == n - 1
  • edges[i].length == 2
  • 0 ≤ ai, bi < n
  • The input represents a valid tree

Visualization

Tap to expand
Tree of Coprimes INPUT Tree Structure: 0 val=2 1 val=3 2 val=3 3 val=2 nums = [2, 3, 3, 2] edges = [[0,1],[1,2],[1,3]] gcd(2,3)=1 (coprime) gcd(2,2)=2 (not coprime) ALGORITHM STEPS 1 Init Ancestor Stacks Stack for each value 1-50 2 DFS from Root Track depth and node 3 Check Coprimes Find closest coprime ancestor 4 Push/Pop Stack Maintain path state Ancestor Stacks: v=2 (0,d0) v=3 (1,d1) At node 2 (val=3): Check stack[2]: (0,d0) gcd(3,2)=1 --> ans[2]=0 At node 3 (val=2): Check stack[3]: (1,d1) gcd(2,3)=1 --> ans[3]=1 FINAL RESULT Closest Coprime Ancestors: 0 ans=-1 1 ans=0 2 ans=0 3 ans=1 [-1, 0, 0, 1] OK - All ancestors found Node 0: root, no ancestor Node 1: gcd(3,2)=1, ans=0 Key Insight: Since node values are limited to 1-50, maintain 50 stacks (one per value) during DFS. For each node, only check stacks of coprime values (precomputed). Track (node, depth) pairs to find the closest ancestor. Time: O(50n) = O(n). Push on enter, pop on exit DFS. TutorialsPoint - Tree of Coprimes | Optimized DFS with Ancestor Stack
Asked in
Google 25 Facebook 18 Microsoft 15
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen