Path Existence Queries in a Graph II Problem
Description
LeetCode Problem 3534.
You are given an integer n representing the number of nodes in a graph, labeled from 0 to n - 1.
You are also given an integer array nums of length n and an integer maxDiff.
An undirected edge exists between nodes i and j if the absolute difference between nums[i] and nums[j] is at most maxDiff (i.e., |nums[i] - nums[j]| <= maxDiff).
You are also given a 2D integer array queries. For each queries[i] = [u_i, v_i], find the minimum distance between nodes u_i and v_i_(.) If no path exists between the two nodes, return -1 for that query.
Return an array answer, where answer[i] is the result of the i^th query.
Note: The edges between the nodes are unweighted.
Example 1:
Input: n = 5, nums = [1,8,3,4,2], maxDiff = 3, queries = [[0,3],[2,4]]
Output: [1,1]
Explanation:
The resulting graph is:

Query Shortest Path Minimum Distance
[0, 3] 0 → 3 1
[2, 4] 2 → 4 1
Thus, the output is [1, 1].
Example 2:
Input: n = 5, nums = [5,3,1,9,10], maxDiff = 2, queries = [[0,1],[0,2],[2,3],[4,3]]
Output: [1,2,-1,1]
Explanation:
The resulting graph is:

| Query | Shortest Path | Minimum Distance |
|---|---|---|
| [0, 1] | 0 → 1 | 1 |
| [0, 2] | 0 → 1 → 2 | 2 |
| [2, 3] | None | -1 |
| [4, 3] | 3 → 4 | 1 |
Thus, the output is [1, 2, -1, 1].
Example 3:
Input: n = 3, nums = [3,6,1], maxDiff = 1, queries = [[0,0],[0,1],[1,2]]
Output: [0,-1,-1]
Explanation:
There are no edges between any two nodes because:
- Nodes 0 and 1: |nums[0] - nums[1]| = |3 - 6| = 3 > 1
- Nodes 0 and 2: |nums[0] - nums[2]| = |3 - 1| = 2 > 1
- Nodes 1 and 2: |nums[1] - nums[2]| = |6 - 1| = 5 > 1
Thus, no node can reach any other node, and the output is [0, -1, -1].
Constraints:
1 <= n == nums.length <= 10^50 <= nums[i] <= 10^50 <= maxDiff <= 10^51 <= queries.length <= 10^5queries[i] == [u_i, v_i]0 <= u_i, v_i < n
Solution
Approach: Sorting + Binary Lifting
Two nodes are adjacent when their values differ by at most maxDiff. After sorting by value, the shortest path from a smaller value to a larger one is found greedily: always jump to the node with the largest value that is still within maxDiff. Precompute with two pointers the first jump f[i][0] of every node, then build a binary lifting table f[i][k] for 2^k jumps. For a query, order the two nodes so that nums[u] <= nums[v]. If u == v the answer is 0, and if the values are equal it is 1. Otherwise use the table to count the largest number of jumps d that still stays below nums[v]; if one more jump still does not reach nums[v], return -1, else return d + 1.
Complexity: O((n + q) log n) time and O(n log n) extra space, where n is the number of nodes and q the number of queries.
class Solution:
def pathExistenceQueries(
self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]]
) -> List[int]:
pairs = sorted((x, i) for i, x in enumerate(nums))
m = 20
f = [[0] * m for _ in range(n)]
r = n - 1
for l in range(n - 1, -1, -1):
while pairs[r][0] - pairs[l][0] > maxDiff:
r -= 1
i, j = pairs[l][1], pairs[r][1]
f[i][0] = j
for k in range(1, m):
f[i][k] = f[f[i][k - 1]][k - 1]
ans = []
for i, j in queries:
if nums[i] > nums[j]:
i, j = j, i
if i == j:
ans.append(0)
continue
if nums[i] == nums[j]:
ans.append(1)
continue
d = 0
for k in range(m - 1, -1, -1):
if nums[f[i][k]] < nums[j]:
d |= 1 << k
i = f[i][k]
if nums[f[i][0]] < nums[j]:
ans.append(-1)
else:
ans.append(d + 1)
return ans