Clean And Simple Code
Complexity\n- Time complexity: O(nlogn)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n^2)\n Add your space complexity here, e.g. O(n) \n
# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n^2)\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 nn = edgeList.length;\n        int len = queries.length;\n        boolean res[] = new boolean[len];\n        int arr[][] = new int[len][4];\n        for(int i = 0;i<len;i++){\n            arr[i][0] = queries[i][0];\n            arr[i][1] = queries[i][1];\n            arr[i][2] = queries[i][2];\n            arr[i][3] = i;\n        }\n        Arrays.sort(arr,(a,b) -> a[2] - b[2]);\n        Arrays.sort(edgeList,(a,b) -> a[2] - b[2]);\n        DSU obj = new DSU(n);\n        int j = 0;\n        for(int i = 0;i<len;i++){\n            int a = arr[i][0];\n            int b = arr[i][1];\n            int dis = arr[i][2];\n            int idx = arr[i][3];\n            while(j<nn && edgeList[j][2] < dis){\n                obj.union(edgeList[j][0], edgeList[j++][1]);\n            }\n            res[idx] = obj.findPar(a) == obj.findPar(b);\n        }\n        return res;\n    }\n}\nclass DSU{\n    int par[];\n    int rank[];\n    DSU(int n){\n        par = new int[n];\n        rank = new int[n];\n        for(int i = 0;i<n;i++){\n            par[i] = i;\n        }\n    }\n    int findPar(int n){\n        if(n == par[n]) return n;\n        return par[n] = findPar(par[n]);\n    }\n    void union(int u, int v){\n        u = findPar(u);\n        v = findPar(v);\n        if(rank[u] < rank[v]){\n            par[u] = v;\n        }else if(rank[v] < rank[u]){\n            par[v] = u;\n        }else{\n            par[v] = u;\n            rank[u]++;\n        }\n    }\n}\n```
['Array', 'Union Find', 'Graph', 'Sorting', 'Java']