Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Advanced Graphs

Greatest Common Divisor Traversal

HARD✓ CHIP-TIMEDLC #2709 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Two array positions are linked whenever their values share a common factor greater than 1. Decide whether every position can reach every other position by hopping across these shared-factor links.

An array of positive integers arrives, and two positions in it are considered linked whenever their values share a common factor greater than one.

Hopping from a position to any other position it's directly linked to, and from there to further linked positions, traces out a reachability web across the whole array.

The task is a single yes-or-no answer: can every position in the array reach every other position by hopping across these shared-factor links, however many hops it takes?

  • array length can run into the tens of thousands
  • values are positive integers, can repeat, and can be large
  • a link exists whenever two values share any prime factor
  • answer is boolean: fully connected or not
EX 01
nums = [7]
→ true
single element, trivially connected
EX 02
nums = [2, 3]
→ false
coprime pair, no shared factor
EX 03
nums = [4, 6]
→ true
direct shared factor of 2

The hintstake only what you need

HINT 1 THE NUDGE

The link rule isn't about the two full values — it's about whether they share even a single prime factor. Two huge numbers are linked the instant one prime divides both.

HINT 2 THE STRUCTURE

Building the actual n² web of links and testing every pair is the honest way in, but a shared prime factor is really a hub: any two numbers that both divide by 5 are linked whether or not you ever check their gcd directly.

HINT 3 ONE STEP FROM THE ANSWER

Union-Find over the numbers AND their prime factors together: union each number's index with every prime that divides it. Numbers sharing a prime automatically land in the same set — no pairwise gcd ever needed. One connected set at the end means yes.

Coach's boardthe pattern, step by step

CONNECTED BY PRIME BRIDGESPATTERN · UNION THROUGH SHARED PRIMESnums = [6, 15, 35, 77]
STEP 1

4 numbers: 6, 15, 35, 77. Two indices connect if they share a prime factor — factor each value and union its index with virtual nodes for its primes.

STEP 1 / 7 · ← → WORK TOO

The splitstwo paces, two languages

grind/greatest-common-divisor-traversal.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def canTraverseAllPairs(self, nums: List[int]) -> bool:
        n = len(nums)
        if n == 1:
            return True
        parent = {}

        def find(x):
            if x not in parent:
                parent[x] = 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:
                parent[ra] = rb

        for i, v in enumerate(nums):
            x = v
            p = 2
            while p * p <= x:
                if x % p == 0:
                    union(i, ('p', p))
                    while x % p == 0:
                        x //= p
                p += 1
            if x > 1:
                union(i, ('p', x))

        roots = {find(i) for i in range(n)}
        return len(roots) == 1
TIME O(N·√(MAXVAL))SPACE O(N + PRIMES)PYTHON · RACE PACE · 34 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED