Data Structures19 sections · 729 units
Open in Course

Problem - Largest Component Size by Factor

Union by common factors

Given an array of unique positive integers, connect two numbers if they share a common factor greater than 1. Return the size of the largest connected component.

Example: [4,6,15,35][4, 6, 15, 35] returns 44. - 44 and 66 share factor 22

  • 66 and 1515 share factor 33
  • 1515 and 3535 share factor 55
  • All four numbers are in one component.

Checking all pairs is O(n2)O(n^2). Can you do better? Constraints: up to 21042 \cdot 10^4 numbers, each up to 10510^5.