Tree of Coprimes - Problem

Imagine you're exploring a family tree where each person has a special numerical value, and you need to find their closest compatible ancestor!

Given a tree with n nodes numbered from 0 to n-1, where node 0 is the root, each node has an associated value. Two values are coprime if their greatest common divisor (GCD) equals 1 - meaning they share no common factors except 1.

Your task: For each node, find its closest ancestor (on the path to root) whose value is coprime with the current node's value. If no such ancestor exists, return -1 for that node.

Input: An array nums where nums[i] is the value of node i, and a 2D array edges representing tree connections.

Output: An array where result[i] is the closest coprime ancestor of node i, or -1 if none exists.

Input & Output

example_1.py โ€” Basic Tree Example
$ Input: nums = [2,3,5,4], edges = [[0,1],[1,2],[1,3]]
โ€บ Output: [-1,0,0,1]
๐Ÿ’ก Note: Node 0 (root) has no ancestors. Node 1 (val=3): ancestor 0 (val=2), gcd(3,2)=1 โœ“. Node 2 (val=5): ancestor 0 (val=2), gcd(5,2)=1 โœ“. Node 3 (val=4): ancestors are 1(val=3) and 0(val=2). gcd(4,3)=1, so closest coprime ancestor is node 1.
example_2.py โ€” No Coprime Ancestors
$ Input: nums = [4,8,12], edges = [[0,1],[0,2]]
โ€บ Output: [-1,-1,-1]
๐Ÿ’ก Note: Root has no ancestors. Node 1 (val=8): ancestor 0 (val=4), gcd(8,4)=4โ‰ 1. Node 2 (val=12): ancestor 0 (val=4), gcd(12,4)=4โ‰ 1. No coprime ancestors exist for nodes 1 and 2.
example_3.py โ€” Deep Tree Chain
$ Input: nums = [5,6,10,2,3,6,15], edges = [[0,1],[1,2],[0,3],[3,4],[3,5],[0,6]]
โ€บ Output: [-1,0,-1,0,3,3,0]
๐Ÿ’ก Note: Complex tree where some nodes have multiple potential coprime ancestors, and we need the closest one. Node 4 (val=3) finds ancestor 3 (val=2) as gcd(3,2)=1, which is closer than root 0 (val=5).

Constraints

  • nums.length == n
  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 50
  • edges.length == n - 1
  • edges[i].length == 2
  • 0 โ‰ค ai, bi < n
  • ai โ‰  bi
  • The given edges form a valid tree

Visualization

Tap to expand
Tree of Coprimes - Family Compatibility2Grandparent (0)3Parent A (1)5Parent B (2)4Child (3)6Child (4)Compatibility CheckChild(4) checks ancestors:โ€ข Parent A(3): gcd(4,3) = 1 โœ“โ€ข Grandparent(2): gcd(4,2) = 2 โœ—ResultChild(3) โ†’ Parent A(1)Child(4) โ†’ Parent A(1)Closest coprime ancestors found!Compatible!
Understanding the Visualization
1
Build the Family Tree
Create connections between family members and establish parent-child relationships
2
Precompute Compatibility
Create a lookup table showing which numbers are coprime (share no factors except 1)
3
DFS Family Traversal
Visit each family member, tracking ancestors by their lucky numbers
4
Find Compatible Mentor
For each person, check their coprime numbers against tracked ancestors to find the closest match
Key Takeaway
๐ŸŽฏ Key Insight: By precomputing coprime relationships and tracking ancestors by value during DFS, we can efficiently find the closest compatible ancestor for each node in O(nร—k) time.
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 18
24.7K Views
Medium Frequency
~25 min Avg. Time
847 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