[JS] Union Find
Approach\nAfter sorting query and edge arrays, we iterate through queries and connect all edges that have a distance below the limit of the current query. Then 
# Approach\nAfter sorting query and edge arrays, we iterate through queries and connect all edges that have a distance below the limit of the current query. Then we check whether the two nodes of the query are connected.\n\n# Complexity\n- Time complexity:\n$$O(n + eloge + qlogq + (e + q))$$ - n to initialize parent and rank arrays, eloge and qlogq to sort edgeList and queriesWithIndex, e + q for union find on every edge and query\n\n- Space complexity:\n$$O(n + q)$$ - n for parent and rank arrays, q for queriesWithIndex\n\n# Code\n```\nvar distanceLimitedPathsExist = function(n, edgeList, queries) {\n    const par = [];\n    for (let i = 0; i < n; i++) par.push(i);\n    const rank = new Array(n).fill(1);\n\n    function find(n) {\n        if (n === par[n]) return n;\n        return par[n] = find(par[n]);\n    }\n\n    function union(n1, n2) {\n        let p1 = find(n1), p2 = find(n2);\n        if (p1 === p2) return false;\n        if (rank[p1] > rank[p2]) {\n            par[p2] = p1;\n            rank[p1] += rank[p2];\n        } else {\n            par[p1] = p2;\n            rank[p2] += rank[p1];\n        }\n        return true;\n    }\n\n    // add index to queries so we know where it goes in result array after sorting\n    const queriesWithIndex = [];\n    for (let i = 0; i < queries.length; i++) {\n        queriesWithIndex[i] = [...queries[i], i];\n    }\n\n    // sort new queries array and edgeList by distance\n    queriesWithIndex.sort((a, b) => a[2] - b[2]);\n    edgeList.sort((a, b) => a[2] - b[2]);\n\n    const res = new Array(queries.length);\n\n    let edgesIndex = 0;\n    // iterate through queries, connecting all edges that are below the limit\n    for (let [p, q, limit, i] of queriesWithIndex) {\n        while (edgesIndex < edgeList.length && edgeList[edgesIndex][2] < limit) {\n            union(edgeList[edgesIndex][0], edgeList[edgesIndex][1]);\n            edgesIndex++;\n        }\n        // check if the two query edges are connected\n        res[i] = (find(p) === find(q));\n    }\n    return res;\n}\n// TC: O(n + eloge + qlogq + (e + q)) n to initialize parent and rank arrays, eloge and qlogq to sort edgeList and queriesWithIndex, e + q for union find\n// SC: O(n + q) n for parent and rank arrays, q for queriesWithIndex\n```
['JavaScript']