Graph Connectivity With Threshold - Problem

Imagine you're a city planner working on a transportation network for n cities numbered from 1 to n. The unique rule for building roads is fascinating: two cities can be directly connected if they share a common divisor greater than a given threshold.

More specifically, cities x and y have a bidirectional road between them if there exists an integer z such that:

  • x % z == 0 (z divides x)
  • y % z == 0 (z divides y)
  • z > threshold

Your task is to answer multiple queries asking whether two cities are connected (directly or through a path of roads). Given n, threshold, and an array of queries where each query is [a, b], return an array of boolean values indicating connectivity.

Goal: Efficiently determine if cities can reach each other through the road network.
Input: Number of cities n, threshold value, and queries array
Output: Boolean array indicating connectivity for each query

Input & Output

example_1.py โ€” Basic connectivity
$ Input: n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]]
โ€บ Output: [false, false, true]
๐Ÿ’ก Note: Cities 1 and 4 have no common divisor > 2. Cities 2 and 5 have no common divisor > 2. Cities 3 and 6 share divisor 3 > 2, so they're connected.
example_2.py โ€” Low threshold
$ Input: n = 6, threshold = 0, queries = [[4,5],[3,4]]
โ€บ Output: [true, true]
๐Ÿ’ก Note: With threshold = 0, many cities are connected. Cities 4 and 5 don't share divisor > 0 directly, but 4 connects to 2,6 and forms a large component. City 3 connects to 6, which connects to other cities.
example_3.py โ€” High threshold edge case
$ Input: n = 5, threshold = 10, queries = [[1,2],[2,3],[1,5]]
โ€บ Output: [false, false, false]
๐Ÿ’ก Note: Since threshold = 10 > n = 5, no cities can be connected through common divisors. Only self-queries would return true.

Constraints

  • 1 โ‰ค n โ‰ค 104
  • 0 โ‰ค threshold โ‰ค n
  • 1 โ‰ค queries.length โ‰ค 105
  • queries[i].length == 2
  • 1 โ‰ค ai, bi โ‰ค n
  • ai != bi

Visualization

Tap to expand
City Transportation Network (threshold = 1)Legend:City (number)Road connectionNetworkCities 2,4,6,8,10,12 connected by common divisor 224681012Cities 3,9 connected by divisor 3395711Isolated (prime numbers)City 6 bridges networks!(divisible by both 2 and 3)๐ŸŽฏ Query Results:โ€ข Query [2,8]: โœ… Connected (same green network) โ€ข Query [3,9]: โœ… Connected (same yellow network)โ€ข Query [5,7]: โŒ Not connected (both isolated) โ€ข Query [4,9]: โœ… Connected (via city 6 bridge)
Understanding the Visualization
1
Identify Common Factors
For each divisor greater than threshold, find all cities (multiples) that share this factor
2
Build Transport Groups
Connect all cities that share the same significant factor using Union-Find
3
Merge Networks
Cities that share multiple factors get connected through overlapping groups
4
Answer Queries
Check if two cities belong to the same connected transportation network
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking all pairs O(nยฒ), group cities by their common divisors using Union-Find. Cities sharing any divisor > threshold will end up in the same connected component, enabling O(1) query responses.
Asked in
Google 42 Meta 38 Amazon 35 Microsoft 28
28.5K Views
Medium-High Frequency
~25 min Avg. Time
867 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