Graph Connectivity Queries Problem
Given a graph with N
nodes and a threshold value THRESHOLDVALUE
, two distinct nodes X
and Y
are directly connected if there exists a Z
such that:
X % Z == 0
Y % Z == 0
Z >= THRESHOLDVALUE
You are given Q
queries. Each query contains two distinct nodes U
and V
. Determine if U
and V
are connected directly or indirectly. Return a list where the i-th
element is 1
if nodes in the i-th
query are connected (either directly or indirectly), otherwise it's 0
.
Example:
Input:
let N be 6 and THRESHOLDVALUE be 2
queries: [(2, 3), (1, 5)]
Output:
1 0
Explanation:
The nodes form connections based on their greatest common divisor (GCD) that is at least the threshold value. For the query with nodes (2, 3), they are indirectly connected, whereas for (1, 5) they are not connected.
Constraints:
1 ≤ T ≤ 10
- Number of test cases1 ≤ N ≤ 100
- Number of nodes1 ≤ THRESHOLDVALUE ≤ 100
- Threshold value1 ≤ Q ≤ 10000
- Number of queries1 ≤ U[i] ≤ N
- First node in the query1 ≤ V[i] ≤ N
- Second node in the query- Time Limit: 1 second
Input:
The first line contains integer 'T' representing number of test cases.
Each test case:
The first line contains integers 'N' and 'THRESHOLDVALUE'.
The second line contains integer 'Q' for number of queries.
Each of the subsequent 'Q' lines contain two integers representing vertices U and V.
Output:
For each test case, output a single line of space-separated integers for answers to all queries.
Note:
You do not need to print anything; it has already been taken care of. Implement the function only.
Be the first one to answer
Add answer anonymously...
Top Visa Fullstack Developer Intern interview questions & answers
Popular interview questions of Fullstack Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app