Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1627: Graph Connectivity With Threshold

In this guide, we solve Leetcode #1627 Graph Connectivity With Threshold in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

We have n cities labeled from 1 to n. Two different cities with labels x and y are directly connected by a bidirectional road if and only if x and y share a common divisor strictly greater than some threshold.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Union Find, Array, Math, Number Theory

Intuition

We need to merge components and check connectivity efficiently.

Union-Find supports near-constant-time merges and finds.

Approach

Initialize each node as its own parent and union pairs as you scan.

Use path compression to keep operations fast.

Steps:

  • Initialize parent arrays.
  • Union related nodes.
  • Use find to check connectivity.

Example

Input: n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]] Output: [false,false,true] Explanation: The divisors for each number: 1: 1 2: 1, 2 3: 1, 3 4: 1, 2, 4 5: 1, 5 6: 1, 2, 3, 6 Using the underlined divisors above the threshold, only cities 3 and 6 share a common divisor, so they are the only ones directly connected. The result of each query: [1,4] 1 is not connected to 4 [2,5] 2 is not connected to 5 [3,6] 3 is connected to 6 through path 3--6

Python Solution

class UnionFind: def __init__(self, n): self.p = list(range(n)) self.size = [1] * n def find(self, x): if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, a, b): pa, pb = self.find(a), self.find(b) if pa == pb: return False if self.size[pa] > self.size[pb]: self.p[pb] = pa self.size[pa] += self.size[pb] else: self.p[pa] = pb self.size[pb] += self.size[pa] return True class Solution: def areConnected( self, n: int, threshold: int, queries: List[List[int]] ) -> List[bool]: uf = UnionFind(n + 1) for a in range(threshold + 1, n + 1): for b in range(a + a, n + 1, a): uf.union(a, b) return [uf.find(a) == uf.find(b) for a, b in queries]

Complexity

The time complexity is O(n×log⁡n×(α(n)+q))O(n \times \log n \times (\alpha(n) + q))O(n×logn×(α(n)+q)), and the space complexity is O(n)O(n)O(n). The space complexity is O(n)O(n)O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy