Skip to content

Practice Graph Connectivity With Threshold

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1627
LeetCode Problem

Graph Connectivity With Threshold

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. More formally, cities with labels x and y have a road between them if there exists an integer z such that all of the following are true: Given the two integers, n and threshold, and an array of queries, you must determine for each queries[i] = [ai, bi] if cities ai and bi are connected directly or indirectly. (i.e. there is some path between them). Return an array answer, where answer.length == queries.length and answer[i] is true if for the ith query, there is a path between ai and bi, or answer[i] is false if there is no path.

Source: Graph Connectivity With Threshold on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]]
Output
[false,false,true]

# Example 2

Input
n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]]
Output
[true,true,true,true,true]

# Example 3

Input
n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]]
Output
[false,false,false,false,false]

Constraints

2 <= n <= 10^4
0 <= threshold <= n
1 <= queries.length <= 10^5
queries[i].length == 2
1 <= ai, bi <= cities
ai != bi

Related LeetCode Problems

Practice more problems similar to Graph Connectivity With Threshold.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.