Graph Connectivity With Threshold - Problem

You 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.

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

  • x % z == 0
  • y % z == 0
  • z > threshold

Given the integers n and threshold, and an array of queries, determine for each queries[i] = [ai, bi] if cities ai and bi are connected directly or indirectly.

Return an array answer, where answer[i] is true if there is a path between ai and bi, or false if no path exists.

Input & Output

Example 1 — Basic Connectivity
$ Input: n = 6, threshold = 2, queries = [[1,4],[4,5],[3,6]]
Output: [false,false,true]
💡 Note: With threshold=2, cities are connected if they share divisor > 2. City 3 and 6 share divisor 3 > 2, so they're connected. Cities 1,4 and 4,5 don't share divisors > 2.
Example 2 — Lower Threshold
$ Input: n = 6, threshold = 0, queries = [[4,5],[4,6],[3,6]]
Output: [true,true,true]
💡 Note: With threshold=0, cities need common divisor > 0. All pairs of cities have gcd ≥ 1, and since 1 > 0, all cities are connected through divisor 1.
Example 3 — No Connections
$ Input: n = 5, threshold = 4, queries = [[2,3],[1,5]]
Output: [false,false]
💡 Note: With high threshold=4, no cities can be connected since largest city is 5 and no pairs share divisor > 4.

Constraints

  • 1 ≤ n ≤ 104
  • 0 ≤ threshold ≤ n
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 2
  • 1 ≤ ai, bi ≤ cities

Visualization

Tap to expand
Graph Connectivity With Threshold INPUT Cities 1-6, threshold=2 1 2 3 4 5 6 n = 6 threshold = 2 queries = [ [1,4], [4,5], [3,6] ] z > 2 connects cities ALGORITHM STEPS 1 Initialize Union-Find parent[i] = i for all cities 2 Find Common Divisors For z from threshold+1 to n 3 Union Multiples Union(z, 2z, 3z...) for z>2 4 Answer Queries Check if find(a) == find(b) Union Operations (z=3,4,5,6): z=3: union(3,6) z=4: union(4) - only one z=5: union(5) - only one z=6: union(6) - only one Components: {1},{2},{3,6},{4},{5} FINAL RESULT Query Evaluations: Query [1,4]: find(1)=1, find(4)=4 Result: false Query [4,5]: find(4)=4, find(5)=5 Result: false Query [3,6]: find(3)=3, find(6)=3 Result: true OUTPUT ARRAY: false false true [false, false, true] Key Insight: Instead of checking all pairs O(n^2), iterate through divisors z > threshold and union all multiples of z. This precomputes connected components in O(n log n) time. Each query then runs in near O(1) with path compression. Cities share divisor z>2 iff both divisible by z. Only 3,6 share divisor 3, so only they connect. TutorialsPoint - Graph Connectivity With Threshold | Union-Find with Precomputed Components
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
23.5K Views
Medium Frequency
~35 min Avg. Time
890 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