Back to July Back to Year 2026
MEDIUM 09 Jul 2026 View on LeetCode

3532. Path Existence Queries in a Graph I

</> Solution

/**
 * Problem: LeetCode 3532 - Path Existence Queries in a Graph I
 * Difficulty: Medium
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public boolean[] pathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) {
        int[] component = new int[n];
        int id = 0;
        component[0] = 0;

        for (int i = 1; i < n; i++) {
            if (nums[i] - nums[i - 1] > maxDiff) {
                id++;
            }
            component[i] = id;
        }

        boolean[] ans = new boolean[queries.length];
        for (int i = 0; i < queries.length; i++) {
            ans[i] = component[queries[i][0]] == component[queries[i][1]];
        }

        return ans;
    }
}

TIME COMPLEXITY

O(N + Q)

SPACE COMPLEXITY

O(N)

TOPICS

Array Graph Union Find