JAVA : Union Find
\nclass Solution {\n    private static class DisJointSet{\n        int val;\n        DisJointSet par;\n        public DisJointSet(int value){\n            this.
```\nclass Solution {\n    private static class DisJointSet{\n        int val;\n        DisJointSet par;\n        public DisJointSet(int value){\n            this.val = value;\n            this.par = this;\n        }\n    }\n\n    private void union(DisJointSet set1, DisJointSet set2){ set1.par = set2; }\n\n    private DisJointSet find(DisJointSet set){\n        if(set.par == set) return set;\n        return set.par = find(set.par);\n    }\n\n    private HashMap<Integer, DisJointSet> hashMap = new HashMap<>();\n    private HashMap<DisJointSet, Set<Integer>> nodeMap = new HashMap();\n    private int nodes;\n    \n    public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n        nodes = n;\n        for (int i = 0; i < nodes; i++)\n            hashMap.put(i, new DisJointSet(i));\n        //sort edges by weight - this is trivial thing to begin Kruskal\n        Arrays.sort(edgeList, Comparator.comparingInt(arr -> arr[2]));\n        \n        //mark index along with query - not a better way as it use space ---use hashmap or something\n        int[][] queriesWithIdx = new int[queries.length][];\n        for (int idx = 0; idx < queries.length; idx++) {\n            int[] query = queries[idx];\n            int[] nquery = new int[]{query[0], query[1], query[2],idx};\n            queriesWithIdx[idx] = nquery;\n        }\n        \n        //sort queries by cost so that we will use edges only till that cost threshold to build MST\n        Arrays.sort(queriesWithIdx, Comparator.comparingInt(arr -> arr[2]));\n        boolean[] res = new boolean[queries.length];\n        int prev = 0;\n        for (int[] query : queriesWithIdx){\n            int costThreshold = query[2];\n            for (int i = prev; i < edgeList.length; i++){\n                if(edgeList[i][2] >= costThreshold){\n                    //record the prev index , so that next run of kruskal should start from edge from that index\n                    prev = i;\n                    break;\n                }\n                int u = edgeList[i][0];\n                int v = edgeList[i][1];\n                union(find(hashMap.get(u)), find(hashMap.get(v)));\n            }\n            //after doing a kruskal on limites set of edges bound by constraint , check if the u and v are in same set or not\n            if(find(hashMap.get(query[0])) == find(hashMap.get(query[1]))){\n                res[query[3]] = true;\n            }else\n                res[query[3]] = false;\n        }\n\n        \n        return  res;\n    }\n}\n```
[]