Simple DSU Solution Explained in Comments ✅
Complexity\n- Time complexity :\n- Space complexity :\n\n# Code\n\nclass DisjointSet {\npublic:\n    vector<int> parent, rank;\n    DisjointSet(int n) {\n      
# Complexity\n- Time complexity :\n- Space complexity :\n\n# Code\n```\nclass DisjointSet {\npublic:\n    vector<int> parent, rank;\n    DisjointSet(int n) {\n        rank.resize(n + 1, 0); \n        parent.resize(n + 1); \n        // Initially, each element is its own parent\n        for (int i = 0; i <= n; i++) {\n            parent[i] = i; \n        }\n    }\n\n    int findParent(int u) {\n        if (u == parent[u]) return u; \n        return parent[u] = findParent(parent[u]); \n    }\n\n    // Union operation by rank\n    void unionByRank(int u, int v) {\n        u = findParent(u);\n        v = findParent(v);\n        // If u and v are already in the same set, do nothing\n        if (u == v) return; \n        if (rank[u] < rank[v]) {\n            parent[u] = v; \n        } else if (rank[v] < rank[u]) {\n            parent[v] = u;\n        } else {\n            parent[u] = v; \n            rank[v]++;\n        }\n    }\n};\n\nclass Solution {\npublic:\n    // Comparator function for sorting edgeList and queries based on distance/limit\n    static bool cmp(vector<int>& a, vector<int>& b) {\n        return a[2] < b[2]; \n    }\n\n    // Function to check if there exists a path between nodes within given distance limits\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {\n        DisjointSet ds(n); \n        int size = edgeList.size(); \n        int k = queries.size(); \n\n        // Add index to queries to keep track of their original position after sorting\n        for (int i = 0; i < k; i++) queries[i].push_back(i);\n\n        // Sort edgeList and queries based on distance limits so that processing done in a way that \n        // edges are processed in increasing order of distance, and queries are processed accordingly\n        sort(edgeList.begin(), edgeList.end(), cmp);\n        sort(queries.begin(), queries.end(), cmp);\n\n        // result vector\n        vector<bool> ans(k, false); \n\n        int i = 0; // Iterator over edgeList\n        int j = 0; // Iterator over queries\n\n        // Process queries\n        while (j < k) {\n            int u = queries[j][0];\n            int v = queries[j][1];\n            int lim = queries[j][2];\n            int idx = queries[j][3]; // Index to update the answer\n\n            // Add edges to Disjoint Set until reaching the current query\'s limit\n            while (i < size && edgeList[i][2] < lim) {\n                ds.unionByRank(edgeList[i][0], edgeList[i][1]);\n                i++;\n            }\n\n           // Check if u and v belong to the same component in Disjoint Set i.e. we can move to any node from one to another so mark that as true\n            if (ds.findParent(u) == ds.findParent(v)) ans[idx] = true;\n\n            // Move to the next query\n            j++; \n        }\n\n        // Return the result\n        return ans; \n    }\n};\n\n```
['Union Find', 'Sorting', 'C++']