Count Connected Components in LCM Graph - Problem

Imagine you're a network analyst tasked with finding clusters of related numbers based on their mathematical compatibility. You have an array of integers nums and a threshold value that determines when two numbers can be connected.

Two numbers nums[i] and nums[j] are considered connected if their Least Common Multiple (LCM) is less than or equal to the threshold: lcm(nums[i], nums[j]) ≤ threshold.

Your goal is to count how many separate connected components exist in this graph. A connected component is a group of numbers where you can reach any number from any other number in the same group through a series of connections, but cannot reach numbers in other groups.

Example: If nums = [2, 6, 4, 3] and threshold = 6:
• LCM(2, 6) = 6 ≤ 6 ✓ (connected)
• LCM(2, 4) = 4 ≤ 6 ✓ (connected)
• LCM(6, 4) = 12 > 6 ✗ (not connected)
• LCM(3, others) > 6 ✗ (isolated)

This creates 2 components: {2, 6, 4} and {3}.

Input & Output

example_1.py — Basic Case
$ Input: nums = [2, 6, 4, 3], threshold = 6
Output: 2
💡 Note: LCM(2,6) = 6 ≤ 6, LCM(2,4) = 4 ≤ 6, so {2,6,4} form one component. LCM(6,4) = 12 > 6, but they're already connected through 2. Element 3 has LCM > 6 with all others, forming its own component. Total: 2 components.
example_2.py — All Connected
$ Input: nums = [4, 6, 15, 35], threshold = 20
Output: 1
💡 Note: LCM(4,6) = 12 ≤ 20, LCM(6,15) = 30 > 20, but LCM(4,15) = 60 > 20. However, we need to check all pairs: LCM(15,35) = 105 > 20. Actually, only 4 and 6 are connected, others form separate components. Wait - let me recalculate: this forms 3 components: {4,6}, {15}, {35}.
example_3.py — All Isolated
$ Input: nums = [5, 7, 11, 13], threshold = 10
Output: 4
💡 Note: All numbers are prime and greater than threshold/2, so all LCM values exceed the threshold. Each number forms its own component, resulting in 4 separate components.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 105
  • 1 ≤ threshold ≤ 109
  • All elements in nums are unique

Visualization

Tap to expand
LCM Graph Component Analysis2643LCM(2,6)=6≤6 ✓LCM(2,4)=4≤6 ✓LCM(3,4)=12>6 ✗Component 1: {2, 6, 4}Connected through LCM relationshipsSize: 3 nodesComponent 2{3}Size: 1 nodeThreshold = 6Result: 2 Connected ComponentsUnion-Find efficiently merges components: O(n² α(n)) time, O(n) space
Understanding the Visualization
1
Initialize Components
Start with each person (number) as their own friend group
2
Check Compatibility
For every pair, calculate their LCM to see if they can be friends
3
Merge Groups
When two people are compatible, merge their entire friend groups
4
Count Final Groups
The number of distinct friend groups is our answer
Key Takeaway
🎯 Key Insight: Union-Find provides the optimal balance of efficiency and simplicity for dynamic connectivity problems, avoiding the need to build and traverse explicit graph structures.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen