Ctrl K

Biggest Shared-Factor Group

Hard
You are given an array values of distinct positive integers. Link two numbers whenever they have a common divisor greater than 1. Links chain together: if a is linked to b and b to c, all three are in the same group, even when a and c share no divisor. Return how many numbers the largest group contains.
Example 1
values=[10, 21, 14, 9, 11] → 4

10 and 14 share 2, 14 and 21 share 7, 21 and 9 share 3, so 10, 14, 21 and 9 form one group; 11 shares no factor with anyone. The answer is 4.

Example 2
values=[8, 25, 27, 49] → 1

Every two of these numbers are coprime, so each group holds a single number.

Example 3
values=[6, 35, 1, 15] → 3
Constraints
  • 1 ≤ len(values) ≤ 2 · 10⁴
  • 1 ≤ values[i] ≤ 10⁵
  • all numbers in values are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsGroups With a Small LCM HardTowns Joined by Big Divisors HardSorting With Shared-Divisor Swaps HardFirst Coprime Cut HardAll Linked by Common Factors Hard
loading Python…
Input
values=[10, 21, 14, 9, 11]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.