Path Existence Queries in a Graph I - Problem

Path Existence Queries in a Graph

Imagine you're building a social network where people can only be friends if their interests are similar enough! You have n users numbered from 0 to n-1, each with a compatibility score stored in the sorted array nums.

Two users i and j can be directly connected (friends) if the absolute difference between their scores is at most maxDiff: |nums[i] - nums[j]| ≤ maxDiff.

But here's the interesting part: users can also be indirectly connected through mutual friends! If user A is friends with user B, and user B is friends with user C, then A and C are connected through B.

Given multiple queries asking whether two users are connected (directly or indirectly), your task is to return a boolean array indicating the connectivity for each query.

Input & Output

example_1.py — Basic connectivity
$ Input: n = 5, nums = [1, 3, 5, 7, 9], maxDiff = 2, queries = [[0, 1], [1, 2], [0, 4]]
› Output: [true, true, false]
šŸ’” Note: Node 0 (value=1) connects to node 1 (value=3) since |1-3|=2 ≤ maxDiff. Node 1 connects to node 2 (value=5). So 0→1→2 forms a path. But node 4 (value=9) is isolated since |7-9|=2 but there's no path from component {0,1,2,3} to {4}.
example_2.py — All connected
$ Input: n = 4, nums = [2, 4, 6, 8], maxDiff = 3, queries = [[0, 3], [1, 2]]
› Output: [true, true]
šŸ’” Note: With maxDiff=3, we have connections: 0↔1 (|2-4|=2), 1↔2 (|4-6|=2), 2↔3 (|6-8|=2). This creates one connected component containing all nodes, so all queries return true.
example_3.py — No connections
$ Input: n = 3, nums = [1, 10, 20], maxDiff = 2, queries = [[0, 1], [0, 2], [1, 2]]
› Output: [false, false, false]
šŸ’” Note: No two nodes can connect since all value differences exceed maxDiff=2. Each node forms its own isolated component, so all connectivity queries return false.

Constraints

  • 2 ≤ n ≤ 105
  • nums.length == n
  • 0 ≤ nums[i] ≤ 109
  • nums is sorted in non-decreasing order
  • 0 ≤ maxDiff ≤ 109
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 2
  • 0 ≤ queries[i][0], queries[i][1] < n

Visualization

Tap to expand
User 0score: 1User 1score: 3User 2score: 5User 3score: 10āœ“ |1-3|≤2āœ“ |3-5|≤2āœ— |5-10|>2Connected CommunityUsers 0, 1, 2 can communicatethrough friendship networkIsolatedUser 3No connectionsmaxDiff = 2 creates friendship boundaries based on compatibility scores
Understanding the Visualization
1
Initialize Users
Each user starts as their own social circle
2
Form Friendships
Connect users whose compatibility scores differ by at most maxDiff
3
Build Communities
Friends-of-friends form larger connected communities
4
Answer Queries
Check if two users belong to the same social community
Key Takeaway
šŸŽÆ Key Insight: Union-Find efficiently groups users into connected communities, allowing instant answers to connectivity queries after initial preprocessing.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
23.8K Views
High Frequency
~25 min Avg. Time
892 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