Back to Month
MEDIUM 09 Jul 2026

3532. Path Existence Queries in a Graph I

</> Solution

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