Largest Cluster of Two-Digit Numbers Linked by Common Digits
Trees & GraphsAuto-graded · 22 test casesLast updated
Find the largest connected cluster in a list of two-digit numbers where links come from shared digits, testing graph connectivity modeling with union-find or digit-hub nodes.
You are given a list nums of two-digit integers. Think of each position in the list as its own item.
Two items are linked if their numbers have at least one decimal digit in common. It does not matter whether the shared digit is in the tens place or the ones place in either number. For instance, 37 and 73 are linked, and so are 41 and 14. Numbers like 20 and 35 are not linked.
A cluster is a collection of items that are tied together through chains of links. If item a is linked to b, and b is linked to c, then a, b, and c are all in the same cluster, even if a and c have no digit in common.
Return the number of items in the largest cluster.
Notes
- Items are identified by position, not by value. If the same value appears several times, each occurrence is a separate item and each one counts toward the size of its cluster.
- A number whose two digits are identical (such as
77) simply contains that single digit. - The digit
0is an ordinary digit (for example,30and50are linked). - A list with a single item has a largest cluster of size
1.
Constraints
1 <= len(nums) <= 100- Each value in
numsis an integer from10to99. - The function returns a single integer.
Examples
Example 1
Input: largest_digit_cluster([55, 58, 25, 45, 66])
Output: 4
Example 2
Input: largest_digit_cluster([12, 23, 34, 45])
Output: 4
Example 3
Input: largest_digit_cluster([11, 11, 22, 33, 44])
Output: 2
Hints
Hint 1
Model each list position as a node and add an edge when two numbers have a digit in common. The answer is the size of the biggest connected component.
Hint 2
Convert each number into the set of digits it contains (or a 10-bit mask). Two numbers are linked exactly when these sets intersect (or the masks AND to a nonzero value).
Hint 3
Merge linked indices with union-find (or run DFS/BFS) while tracking component sizes. Do not deduplicate values, since equal numbers are separate items.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat every index of the list as a node in a graph, and connect two nodes when their numbers share a digit. The answer is the size of the largest connected component.
- Digit masks. Convert each number to a 10-bit mask where bit
dis set if digitdappears in the number. Two numbers share a digit exactly when the bitwise AND of their masks is nonzero. This handles tens/ones positions, repeated digits like77, and the digit0uniformly. - Union-Find with sizes. Check every pair of indices
(i, j). If their masks intersect, union the two indices. Union by size keeps trees shallow, and path halving makesfindnearly constant time. - Answer. Return the largest value in
size.
Why it is correct
- A nonzero AND of masks means some digit is present in both numbers, which is exactly the definition of a link.
- Union-find merges components transitively, so after processing all pairs, two indices share a root if and only if a chain of links connects them, which is the definition of a cluster.
- We union indices rather than values, so duplicate numbers each count as separate items.
- Only root entries of
sizeare exact. When a root is attached under another, its stale value is never larger than the size of the root that absorbed it, so the maximum over the whole array equals the maximum over roots.
Complexity
- Time: O(n^2 * alpha(n)) for the pairwise comparisons, plus O(n) to build masks. With n <= 100 this is at most about 4,950 comparisons.
- Space: O(n).
A scaling alternative is to union each item with up to two "digit hub" nodes (one per digit it contains), giving near-linear time.
Reference solution
from typing import List
def largest_digit_cluster(nums: List[int]) -> int:
n = len(nums)
parent = list(range(n))
size = [1] * n
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
def union(a, b):
ra, rb = find(a), find(b)
if ra == rb:
return
if size[ra] < size[rb]:
ra, rb = rb, ra
parent[rb] = ra
size[ra] += size[rb]
masks = []
for value in nums:
mask = 0
for ch in str(value):
mask |= 1 << int(ch)
masks.append(mask)
for i in range(n):
for j in range(i + 1, n):
if masks[i] & masks[j]:
union(i, j)
return max(size) if n else 0