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