Java Union Find Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nSorting the Queries + Computing UNION-FIND of the Edgelist via Sorted Approach.\n# Appr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSorting the Queries + Computing UNION-FIND of the Edgelist via Sorted Approach.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n        int[][] modifiedQueries = new int[queries.length][4];\n        for(int i=0;i<queries.length;i++) {\n            modifiedQueries[i][0] = queries[i][0];\n            modifiedQueries[i][1] = queries[i][1];\n            modifiedQueries[i][2] = queries[i][2];\n            modifiedQueries[i][3] = i;\n        }\n        Arrays.sort(modifiedQueries,(a, b) -> a[2] - b[2]);\n        Arrays.sort(edgeList, (a, b) -> a[2] - b[2]);\n        int i = 0, j = 0;\n        boolean[] answer = new boolean[modifiedQueries.length];\n        DisjointSet disjointSet = new DisjointSet(n);\n\n        while(i<modifiedQueries.length) {\n            while(j < edgeList.length && edgeList[j][2] < modifiedQueries[i][2]) {\n                disjointSet.union(edgeList[j][0], edgeList[j][1]);\n                j++;\n            }\n            answer[modifiedQueries[i][3]] = disjointSet.findParent(modifiedQueries[i][0]) == disjointSet.findParent(modifiedQueries[i][1]);\n            i++;\n        }\n        return answer;\n    }\n}\n\nclass DisjointSet {\n\n    private int[] parent;\n    private int[] rank;\n\n    public DisjointSet(int n) {\n        this.parent = new int[n];\n        this.rank = new int[n];\n        for(int i=1;i<n;i++) {\n            this.parent[i] = i;\n        }\n    }\n\n    public int findParent(int node) {\n        if(this.parent[node]!=node) this.parent[node] = findParent(this.parent[node]);\n        return this.parent[node];\n    }\n\n    public void union(int u, int v) {\n        int uPar = findParent(u);\n        int vPar = findParent(v);\n\n        if(uPar == vPar) return;\n        if(this.rank[uPar] < this.rank[vPar]) this.parent[uPar] = vPar;\n        else if(this.rank[vPar] < this.rank[uPar]) this.parent[vPar] = uPar;\n        else {\n            this.parent[uPar] = vPar;\n            this.rank[vPar]++;\n        }\n    }\n\n}\n```
['Java']