#1766

Tree of Coprimes

master · 1600 · lc hard +32 · verified · 43.9% accepted · 427 likes · top 26%

Description

A tree of n nodes rooted at 0 is given by nums (values) and edges. For each node i, find the closest ancestor j with gcd(nums[i], nums[j]) == 1, or -1 if none. Return an array ans of size n.

Code

1
2
3