1052. Largest Component Size by Common Factor

MediumGraphsDSUMathGraph

Given an array of distinct positive integers 'nums', build a graph where two numbers are connected if they share a common factor greater than 1. Return the size of the largest connected component. The input is JSON {nums}.

Input: JSON {nums}.

Output: Integer — the largest component size.

Examples

Example 1
Input: {"nums":[4,6,15,35]}
Output: 4
Explanation: All connect through shared factors.
Example 2
Input: {"nums":[20,50,9,63]}
Output: 2
Explanation: Two pairs of size 2.
Example 3
Input: {"nums":[2,3,6,7,4,12,21,39]}
Output: 8
Explanation: All in one component.

Constraints

Asked by

GoogleMicrosoft
Solve this problem in the editor →