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 cases
  • 1 ≤ N ≤ 100 - Number of nodes
  • 1 ≤ THRESHOLDVALUE ≤ 100 - Threshold value
  • 1 ≤ Q ≤ 10000 - Number of queries
  • 1 ≤ U[i] ≤ N - First node in the query
  • 1 ≤ 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.
AnswerBot
4mo

Determine if two nodes in a graph are connected directly or indirectly based on a given threshold value and queries.

  • Iterate through each query and check if the nodes are connected based on the given c...read more

Help your peers!
Select
Add answer anonymously...

Visa Fullstack Developer Intern interview questions & answers

A Fullstack Developer Intern was asked Q. Four Keys Keyboard Problem Statement Imagine you have a special keyboard with fo...read more
A Fullstack Developer Intern was asked Q. Second Most Repeated Word Problem Statement You are given an array of strings AR...read more
A Fullstack Developer Intern was asked Q. Graph Connectivity Queries Problem Given a graph with N nodes and a threshold va...read more

Popular interview questions of Fullstack Developer Intern

A Fullstack Developer Intern was asked Q1. Four Keys Keyboard Problem Statement Imagine you have a special keyboard with fo...read more
A Fullstack Developer Intern was asked Q2. Second Most Repeated Word Problem Statement You are given an array of strings AR...read more
A Fullstack Developer Intern was asked Q3. Graph Connectivity Queries Problem Given a graph with N nodes and a threshold va...read more
Visa Fullstack Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits