Optimized UF. Let's learn + dry run.
Intuition\nif you can see application of UF here, you are on the right track.\n> cannot become friends, either directly or indirectly through other people.\n\nT
# Intuition\nif you can see application of UF here, you are on the right track.\n> cannot become friends, either directly or **indirectly** through other people.\n\nThe word **indirectly** basically hints to **Union Find** solution.\n\n- Groups\n- Friends\n- Restrictions\n- Queries\n\nit all should ignite the spark of Union Find in your head if you are familiar with this data structure.\n\nIf not, check some resources:\n- https://www.geeksforgeeks.org/introduction-to-disjoint-set-data-structure-or-union-find-algorithm/\n- https://www.youtube.com/watch?v=wU6udHRIkcc&t=1210s\n\n\n# Approach\n- initially all people are by themselves\n> become direct friends for all future friend requests\n- so, traverse through requests and check if **U** can be friends with **V**. In other words check if we can **union(u, v)**\n\n# check\nWe need to check:\n- **u and v can be direct friends** (same as 2)\n- **if friend any pair from requests has formed a group that if we union(u,v) any pair from restrictions will become connected**\n\n---\n- initially U and V can be friends\n```\nlet areWeFriends = true\n```\n- find groups of **U** and **V**\n```\nconst [uRep, vRep] = [find(u), find(v)]\n```\n- traverse through all restrictions. We have to do it for each request, since ...\n\n- find groups of people from restrictions\n```\nconst [aRep, bRep] = [find(a), find(b)]\n```\n\n# walk through example\n```\nn = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]\n\nDSU: {0}, {1}, {2}, {3}, {4}\n\n\n```\n```\n-------------------\n\nreq = [0, 4]\nuRep, vRep = [0, 4] -> they belong to these groups\nwhat if we connect 0 and 4?\nfind groups of all restriction pair\n-> [0, 1]: [0, 1]\n-> [1, 2]: [1, 2]\n-> [2, 3]: [2, 3]\n\nso they can become friends -> union(0, 4)\nDSU: {0, 4}, {1}, {2}, {3}\n\n-------------------\n\nreq = [1, 2]\nuRep, vRep = [1, 2] -> they belong to these groups\nwhat if we connect 1 and 2?\nwe will form: \nDSU: {0, 4}, {1, 2}, {3}\nfind groups of all restriction pair\n-> [0, 1]: [0, 1] fine\n-> [1, 2]: [1, 2] -> 1 and 2 cannot be in one group\nif we union (1, 2) -> we will create {1, 2} set -> we can\'t union\nDSU: {0, 4}, {1}, {2}, {3}\n\n-------------------\n\nreq = [3, 1]\nuRep, vRep = [3, 1] -> they belong to these groups\n\nwhat if we connect 3 and 1?\nwe will form: \nDSU: {0, 4}, {1, 3}, {2}\n\nfind groups of all restriction pair:\n\n-> [0, 1]: [0, 1] fine\n-> [1, 2]: [1, 2] fine\n-> [2, 3]: [2, 3] fine\n\nunion(3, 1)\n\nDSU: {0, 4}, {1, 3}, {2}\n\n-------------------\n\nreq = [3, 4]\nuRep, vRep = [1, 0] -> they belong to these groups, meaning\nif (1, 0) will be in the same group \n\nwhat if we connect 3 and 4?\nwe will form: \nDSU: {0, 4, 1, 3}, {2}\n\ncan we allow it?\n\nIF ANY OF THESE GROUPS CANNOT EXIST, meaning\nCONTAINS PAIR OF PEOPLE THAT CANNOT BE TOGETHER\n\nTHEN DO NOT UNION(U, V)\nELSE UNION(U, V)\n\nfind groups of all restriction pair:\n\n-> [0, 1]: [0, 1]  -> we see that 0 and 1 cannot be in one group\n                        so do not union(3, 4)\n\nDSU: {0, 4}, {1, 3}, {2}\n\nans = [true, false, true, false]\n```\n\n```\n                              THANK YOU\n```\n\n# Code\n```\n/**\n * @param {number} n\n * @param {number[][]} restrictions\n * @param {number[][]} requests\n * @return {boolean[]}\n */\nvar friendRequests = function(n, restrictions, requests) {\n    const parent = Array(n)\n    const size = Array(n)\n    for (let i = 0; i < n; i++) {\n        parent[i] = i\n        size[i] = 1\n    }\n\n    const find = i => parent[i] === i ? i : find(parent[i])\n\n    const union = (x, y) => {\n        const [xRep, yRep] = [find(x), find(y)]\n        if (xRep === yRep) return\n\n        if (size[xRep] > size[yRep]) {\n            parent[yRep] = xRep\n            size[xRep] += size[yRep]\n        }\n        else if (size[yRep] > size[xRep]) {\n            parent[xRep] = yRep\n            size[yRep] += size[xRep]\n        }\n        else {\n            parent[yRep] = xRep\n            size[xRep]++\n        }\n    }\n\n    const ans = []\n\n    for (const [u, v] of requests) {\n        let areWeFriends = true\n        const [uRep, vRep] = [find(u), find(v)]\n        for (const [a, b] of restrictions) {\n            const [aRep, bRep] = [find(a), find(b)]\n            if (\n                (uRep === aRep && vRep === bRep) ||\n                (uRep === bRep && vRep === aRep)\n               ) \n            {\n                ans.push(false)\n                areWeFriends = false\n                break\n            }\n        }\n        if (areWeFriends) {\n            union(u, v)\n            ans.push(true)\n        }\n        \n    }\n    return ans\n};\n```
['Union Find', 'JavaScript']