Java | Passes all test cases | Union Find
\nclass Solution {\n        public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n            int[] parent = new int[n];\n\n  
```\nclass Solution {\n        public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n            int[] parent = new int[n];\n\n            for (int i = 0; i < n; ++i) {\n                parent[i] = i;\n            }\n\n            // Sort the edges in the provided graph based on distance.\n            Arrays.sort(edgeList, Comparator.comparing(a -> Integer.valueOf(a[2])));\n\n            // Sort queries in increasing order of distance.\n            List<int[]> q = new ArrayList<>();\n            for (int i = 0; i < queries.length; ++i) {\n                int[] entry = new int[4];\n                entry[0] = queries[i][0]; // Start index\n                entry[1] = queries[i][1]; // End Index\n                entry[2] = queries[i][2]; // Threshold\n                entry[3] = i; //\n\n                q.add(entry);\n            }\n\n            q.sort(Comparator.comparing(a -> a[2]));\n\n            int index = 0;\n            boolean[] result = new boolean[q.size()];\n            for (var entry : q) {\n                int threshold = entry[2];\n                //\n                while (index < edgeList.length && edgeList[index][2] < threshold) {\n                    union(parent, edgeList[index][0], edgeList[index][1]);\n                    ++index;\n                }\n\n                result[entry[3]] = find(parent, entry[0]) == find(parent, entry[1]);\n            }\n\n            return result;\n        }\n\n        // O(Log(N))\n        int find(int[] parent, int ind) {\n            if (parent[ind] != ind) {\n                return find(parent, parent[ind]);\n            }\n\n            return ind;\n        }\n\n        // O(Log(N))\n        void union(int[] parent, int x, int y) {\n            int p1 = find(parent, x);\n            int p2 = find(parent, y);\n            parent[p1] = p2;\n        }\n    }\n```
[]