Easy Solution Of JAVA 🔥C++🔥Beginner Friendly With Comments🔥
\n\n# Code\nPLEASE UPVOTE IF YOU LIKE.\n\nclass Solution {\n    // union find\n    // sort both edgeList and queries in increasing order of dist and limit, resp
\n\n# Code\nPLEASE UPVOTE IF YOU LIKE.\n```\nclass Solution {\n    // union find\n    // sort both edgeList and queries in increasing order of dist and limit, respectively\n    // two loops:\n    // outer loop: for each query, if limit increases, than check to see if there is more edges that\n    // can be added\n    // inner loop: if dis is less than the new limit, add the edge\n\n    // Runtime is bound by sorting: O(ElogE + NlogN + N + E);\n    int[] up;\n    \n    public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n        up = new int[n];\n        Arrays.fill(up, -1);\n        \n        boolean[] res = new boolean[queries.length];\n        Arrays.sort(edgeList, new arrComparator());\n        \n        \n        int[][] temp = new int[queries.length][4];\n        for (int i = 0; i < temp.length; i++) {\n            temp[i][0] = queries[i][0];\n            temp[i][1] = queries[i][1];\n            temp[i][2] = queries[i][2];\n            temp[i][3] = i;\n        }\n        queries = temp;\n        Arrays.sort(queries, new arrComparator());\n        \n        int limit = 0;\n        int j = 0;\n        for (int i = 0; i < queries.length; i++) {\n            int[] q = queries[i];\n            if (q[2] > limit) {\n                limit = q[2];\n                while (j < edgeList.length && edgeList[j][2] < limit) {\n                    int x = edgeList[j][0];\n                    int y = edgeList[j][1];\n                    if (find(x) != find(y)) union(find(x), find(y));\n                    j++;\n                }\n            }\n            res[q[3]] = find(q[0]) == find(q[1]);\n        }\n        return res;\n    }\n    \n    private int find(int x) {\n        int r = x;\n        while (up[r] >= 0) {\n            r = up[r];\n        }\n        \n        if (r == x) return r;\n        \n        int p = up[x];\n        while (p >= 0) {\n            up[x] = r;\n            x = p;\n            p = up[x];\n        }\n        return r;\n    }\n    \n    private void union(int x, int y) {\n        if (up[x] > up[y]) {\n            up[y] += up[x];\n            up[x] = y;\n        } else {\n            up[x] += up[y];\n            up[y] = x;\n        }\n    }\n    \n    class arrComparator implements Comparator<int[]> {\n        public int compare(int[] a, int[] b) {\n            return a[2] - b[2];\n        } \n    }\n}\n```\nC++\n```\nclass DSUnode{\n    public:\n    int parent,rank;\n    DSUnode(int parent,int rank){\n        this->parent=parent;\n        this->rank=rank;\n    }\n};\nclass DSU{\n    private:\n    vector<DSUnode>parents;\n    void _merge(int parent,int child){\n        this->parents[child].parent=parent;\n        this->parents[parent].rank+=this->parents[child].rank;\n    }\n    int _find(int x){\n        while(this->parents[x].parent!=x){\n            x=this->parents[x].parent;\n        }\n        return x;\n    }\n    public:\n    DSU(int n){\n        for(int i=0;i<n;i++){\n            DSUnode element(i,1);\n            this->parents.push_back(element);\n        }\n    }\n    void findUnion(int x,int y){\n        int parx=this->_find(x);\n        int pary=this->_find(y);\n        if(parx!=pary){\n            if(this->parents[parx].rank>=this->parents[pary].rank){\n                this->_merge(parx,pary);\n            } else {\n                this->_merge(pary,parx);\n            }\n        }\n    }\n    bool isConnected(int start,int end){\n        return this->_find(start)==this->_find(end);\n    }\n};\nclass Solution {\nprivate:\n    int determineEnd(vector<int>&weights,int limit){\n        int end=lower_bound(weights.begin(),weights.end(),limit)-weights.begin();\n        return end==weights.size()?end:end-1;\n    }\n    bool solve(DSU &dsu,int qs,int qe,int &start,int end,vector<vector<int>>&edges){\n        if(dsu.isConnected(qs,qe)){\n            return true;\n        } else {\n            while(start<=end){\n                int s=edges[start][0];\n                int e=edges[start][1];\n                dsu.findUnion(s,e);\n                if(dsu.isConnected(qs,qe)){\n                    return true;\n                }\n                start++;\n            }\n            return false;\n        }\n    }\npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {\n        DSU dsu(n);\n        for(int i=0;i<queries.size();i++){\n            queries[i].push_back(i);\n        }\n        sort(edges.begin(),edges.end(),[](vector<int>&e1,vector<int>&e2){\n            return e1[2]<e2[2];\n        });\n        sort(queries.begin(),queries.end(),[](vector<int>&q1,vector<int>&q2){\n            return q1[2]<q2[2];\n        });\n        vector<int>weights;\n        for(vector<int>edge:edges){\n            weights.push_back(edge[2]);\n        }\n        vector<bool>ans(queries.size(),true);\n        int start=0;\n        for(vector<int>query:queries){\n            int qs=query[0];\n            int qe=query[1];\n            int limit=query[2];\n            int index=query[3];\n            int end=determineEnd(weights,limit);\n            ans[index]=solve(dsu,qs,qe,start,end,edges);\n        }\n        return ans;\n    }\n};\n```
['Sorting', 'Java']