Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Largest Component Size by Common Factor

XPChallenge Points: 30
levelLevel: Hard

You are given an array of distinct positive integers nums.
Build a graph where:


• Each number in nums is a node.
• There is an undirected edge between nums[i] and nums[j] if they share a common factor greater than 1.
Return the size of the largest connected group in this graph.

Example 1:

Input: nums = [4,6,15,35]

Output: 4

Explanation:

• Factors connect as: 4 ↔ 6 (factor 2), 6 ↔ 15 (factor 3), 15 ↔ 35 (factor 5). • All four numbers are connected through shared factors, forming a single group of size 4.

Example 2:

Input: nums = [20,50,9,63]

Output: 2

Explanation:

• 20 and 50 share factor 10 → group of size 2. • 9 and 63 share factor 3 → another group of size 2.

Example 3:

Input: nums = [2,3,6,7,4,12,21,39]

Output: 8

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result