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
Visualization
Time & Space Complexity
O(n²) to check all pairs and calculate LCM, plus O(n + edges) for DFS traversal
O(n²) for adjacency list storage in worst case where all nodes are connected
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 105
- 1 ≤ threshold ≤ 109
- All elements in nums are unique